This project is a microservice for chat completions using the OpenAI API, following Clean Architecture principles. It stores chat sessions and messages in PostgreSQL and exposes a REST API for creating and continuing chats.
- Create new chat sessions or continue existing ones
- Stores chat and message history in PostgreSQL
- Clean, testable architecture
- 100% code coverage in tests
- Node.js (v18+ recommended)
- Docker & Docker Compose
- OpenAI API key
git clone https://github.com/yourusername/chatgpt-microservice-ts.git
cd chatgpt-microservice-ts
Copy .env.example
to .env
and fill in your values:
OPENAI_API_KEY=your-openai-api-key
...
docker compose up -d --build
This will start a PostgreSQL 15 container and initialize the schema.
npm install
npm run start
The API will be available at http://localhost:SERVER_PORT
.
Create a new chat or continue an existing one.
{
"chatId": "optional-chat-id", // Omit or set to continue a chat
"userId": "user-uuid",
"userMessage": "Hello, how are you?",
"config": {
"model": "gpt-3.5-turbo",
"max_tokens": 100,
"temperature": 0.7,
"stop": ["super-end"]
}
}
chatId
: Omit to start a new chat, or provide to continue an existing chat.userId
: Your user identifier.userMessage
: The message to send.config
: (Optional) Chat configuration (model, tokens, etc.).
{
"chatId": "uuid-of-chat",
"messages": [
{ "role": "system", "content": "..." },
{ "role": "user", "content": "Hello, how are you?" },
{ "role": "assistant", "content": "I'm fine, thank you!" }
]
}
chatId
: Use this for subsequent requests to continue the conversation.messages
: Full chat history.
- Start a new chat (omit
chatId
). - Use the returned
chatId
for the next message.
npm test
src/domain
— Entities, models, use cases, repository interfacessrc/infra
— DB, API adapters, migrationssrc/main
— Server setup, routes, configssrc/presentation
— Controllers, http handling
- If you get
relation "chats" does not exist
, ensure your migrations ran. Try:docker compose down -v docker compose up -d --build
- Ensure your
.env
values are valid JSON where required (e.g.,STOP
). - If you see a deprecation warning related to
punycode
there is nothing to do. It is a problem for theopenai
npm package and its dependencies (specifically the dependency tr46 which uses the deprecated punycode module).
- Michael Dias - mikaws
MIT