|
| 1 | +# OpenAI Agents SDK with MongoDB Memory |
| 2 | + |
| 3 | +This demo app shows how to use the [OpenAI Agents SDK](https://openai.github.io/openai-agents-js/) with [MongoDB](https://www.mongodb.com/cloud/atlas/register/?utm_campaign=devrel&utm_source=third-party-content&utm_medium=cta&utm_content=openai-agents-sdk-template&utm_term=jesse.hall) for persistent conversation history. |
| 4 | + |
| 5 | +## Key Features |
| 6 | + |
| 7 | +- Uses MongoDB to store conversation history |
| 8 | +- Leverages the SDK's built-in message handling capabilities |
| 9 | +- Custom tool to access user profile information |
| 10 | +- Simple implementation for educational purposes |
| 11 | + |
| 12 | +## Getting Started |
| 13 | + |
| 14 | +1. Download this app folder using: |
| 15 | + ```bash |
| 16 | + npx degit mongodb-developer/GenAI-Showcase/apps/openai-agents-sdk my-openai-agent |
| 17 | + ``` |
| 18 | + |
| 19 | +2. Make sure MongoDB is set up and running |
| 20 | + |
| 21 | +3. Copy the example environment file and add your credentials: |
| 22 | + ```bash |
| 23 | + cp .env.example .env |
| 24 | + ``` |
| 25 | + Then update the `.env` file with your information: |
| 26 | + ``` |
| 27 | + MONGODB_URI=mongodb://localhost:27017/agent_memory |
| 28 | + OPENAI_API_KEY=your_openai_api_key |
| 29 | + ``` |
| 30 | + |
| 31 | +4. Install dependencies: |
| 32 | + ```bash |
| 33 | + npm install |
| 34 | + ``` |
| 35 | + |
| 36 | +5. Build the TypeScript: |
| 37 | + ```bash |
| 38 | + npm run build |
| 39 | + ``` |
| 40 | + |
| 41 | +6. Run the demo: |
| 42 | + ```bash |
| 43 | + npm start |
| 44 | + ``` |
| 45 | + |
| 46 | +## How It Works |
| 47 | + |
| 48 | +The demo shows a hybrid approach to conversation memory: |
| 49 | + |
| 50 | +1. **MongoDB for Persistence**: Messages are saved to MongoDB so they persist across server restarts |
| 51 | +2. **SDK's Message Handling**: We use the SDK's built-in `user()` and `assistant()` functions to format message history |
| 52 | +3. **Context-Aware Tools**: Custom tools can access application context including user profiles |
| 53 | +4. **Simple Interface**: Clean implementation with minimal code |
| 54 | + |
| 55 | +## Demo Conversation |
| 56 | + |
| 57 | +The demo runs a simple conversation that demonstrates: |
| 58 | +1. Basic question answering |
| 59 | +2. Memory of previous questions (asking about "its population" after asking about France) |
| 60 | +3. Using a tool to fetch user profile information |
| 61 | + |
| 62 | +## Project Structure |
| 63 | + |
| 64 | +- `src/db.ts`: MongoDB connection and conversation storage functions |
| 65 | +- `src/agent.ts`: OpenAI Agents SDK implementation with custom tools |
| 66 | +- `src/types.ts`: TypeScript type definitions for conversations and context |
| 67 | +- `src/index.ts`: Demo script that runs a sample conversation |
| 68 | + |
| 69 | +## License |
| 70 | + |
| 71 | +MIT |
0 commit comments