> ## Documentation Index
> Fetch the complete documentation index at: https://memwirelabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI

> Use OpenAI GPT models with MemWire memory context.

## Prerequisites

* An [OpenAI API key](https://platform.openai.com/api-keys)
* `openai` Python package

```bash theme={null}
pip install openai
```

***

## Quickstart

```python theme={null}
from openai import OpenAI
from memwire import MemWire, MemWireConfig

client = OpenAI(api_key="your-api-key")

config = MemWireConfig(qdrant_path="./memwire_data")
memory = MemWire(config=config)

USER_ID = "alice"

# Store a message into memory
memory.add(
    user_id=USER_ID,
    messages=[{"role": "user", "content": "I prefer dark mode and short answers."}],
)

# Recall relevant context for the next query
result = memory.recall("How should I format my answers?", user_id=USER_ID)

# Build the prompt with injected memory context
messages = [{"role": "system", "content": "You are a helpful assistant."}]
if result.formatted:
    messages.append({"role": "system", "content": f"Memory context:\n{result.formatted}"})
messages.append({"role": "user", "content": "How should I format my answers?"})

# Call the OpenAI API
response = client.chat.completions.create(model="gpt-4o", messages=messages)
reply = response.choices[0].message.content
print(reply)

# Reinforce memory paths that led to this response
memory.feedback(response=reply, user_id=USER_ID)

memory.close()
```

***

## Using environment variables

```bash theme={null}
export OPENAI_API_KEY=your-api-key
```

```python theme={null}
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY automatically
```

***

## Streaming responses

```python theme={null}
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    stream=True,
)

reply = ""
for chunk in stream:
    delta = chunk.choices[0].delta.content or ""
    print(delta, end="", flush=True)
    reply += delta

# Reinforce after the full response is assembled
memory.feedback(response=reply, user_id=USER_ID)
```

***

## Supported models

| Model           | Notes                                   |
| --------------- | --------------------------------------- |
| `gpt-4o`        | Recommended — fast, capable, multimodal |
| `gpt-4o-mini`   | Cheaper, slightly lower quality         |
| `gpt-4-turbo`   | High quality, higher cost               |
| `gpt-3.5-turbo` | Fastest and cheapest                    |

***

## Full working example

See [examples/openai/](https://github.com/memoryoss/memwire/tree/main/examples/openai) for a complete FastAPI web chat example with Docker.
