# Open AI

### 1. Why Use Open AI?

Open AI provides high-quality embedding models that convert text into vector representations capturing semantic meaning. In RECA, these embeddings allow memory to be searched based on *meaning* rather than exact wording.

**Without embeddings:**\
Searching "How long do sharks live?" won’t match "Sharks have existed for 400 million years."

**With embeddings:**\
That query will match based on meaning, not just shared words.

***

### 2. Enabling Open AI Embeddings in RECA

To activate Open AI embeddings, pass the `OpenAIEmbedder` into your `Memory` object:

```python
pythonCopyEditfrom reca.memory.core import Memory
from reca.models.embedding import OpenAIEmbedder

mem = Memory(embedder=OpenAIEmbedder())
```

Now, all inserted memory and all search queries will be embedded using Open AI’s API and compared semantically.

***

### 3. Authentication

Set your Open AI API key in the environment before running any code:

```
arduinoCopyEditexport OPENAI_API_KEY=your-api-key-here
```

If you're using a `.env` file, you can load it automatically with the `dotenv` package.

***

### 4. Example: Semantic Search

```python
pythonCopyEditmem.insert("Einstein developed the theory of relativity.")
mem.insert("Isaac Newton formulated the laws of motion.")

query = "Who created the concept of gravity?"
results = mem.search(query)
print(results[0].text)
```

Even though the query doesn’t mention "Newton," semantic matching makes the connection.

***

### 5. Configuration Options

You can pass optional arguments to customize the embedder:

```python
pythonCopyEditOpenAIEmbedder(
  model="text-embedding-3-small",  # or use 3-large for more accuracy
  batch_size=8,                    # for chunked memory parsing
  max_retries=3                    # retry logic for API failures
)
```

***

### 6. Cost and Performance

Using OpenAI embeddings incurs API costs and latency.

| Model                  | Dimensionality | Cost per 1K tokens | Use Case             |
| ---------------------- | -------------- | ------------------ | -------------------- |
| text-embedding-3-small | 1536           | Low                | Fast, cheap matching |
| text-embedding-3-large | 3072           | Higher             | Higher accuracy      |

To reduce cost:

* Avoid embedding very large documents at once
* Reuse embeddings for static data
* Batch embed chunks where possible

***

### 7. Caching Embeddings (Optional)

To avoid redundant API calls, you can implement a simple cache layer around the embedder. RECA doesn’t include one by default, but it's easy to wrap:

```python
CachedEmbedder(OpenAIEmbedder):
    def __init__(self):
        super().__init__()
        self.cache = {}

    def embed(self, text):
        if text in self.cache:
            return self.cache[text]
        vec = super().embed(text)
        self.cache[text] = vec
        return vec
```

***

### 8. Alternatives and Fallbacks

If Open AI isn’t viable for your use case (cost, privacy, offline use), RECA supports custom embedding backends. You can replace Open AI with a local model like:

* Sentence Transformers (e.g., `all-MiniLM`)
* Cohere or Mistral APIs
* Custom HF transformers

RECA’s architecture makes it easy to swap in any embedder you define.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://credie.gitbook.io/recaengine/basics/openapi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
