This is a LangChain + OpenAI powered Translator Chatbot. It takes input in English and translates it into a target language of your choice.
The project demonstrates:
- Setting up and using LangChain with JavaScript
- Creating and using prompt templates
- Making direct LLM calls and streaming responses
- Building a foundation for an interactive CLI chatbot
langchain-chatbot/
βββ README.md # Documentation
βββ package.json # Project dependencies and scripts
βββ .env # Environment variables (ignored in git)
βββ .env.example # Example environment variables file
βββ .gitignore # Ignored files and folders
βββ index.js # Entry point (chatbot runner)
βββ /src
β βββ model.js # Model configuration (OpenAI setup)
β βββ prompt.js # Prompt template logic
β βββ chat.js # Chat / translation logic
git clone <repo-url>
cd langchain-chatbotnpm installRequired packages:
Copy the example file and edit with your own keys:
cp .env.example .env# OpenAI API Key (Required)
OPENAI_API_KEY=your-openai-api-key-here
# LangSmith tracing (Optional, set to "true" to enable)
LANGSMITH_TRACING=true
# LangSmith API Key (Optional, only if using LangSmith)
LANGSMITH_API_KEY=your-langsmith-api-key-here.env to git.
Configures the LLM model using OpenAI:
- Loads API key via
dotenv. - Creates an instance of
ChatOpenAI. - Sets parameters (
model: gpt-4o-mini,temperature: 0). - Exports the model to be used by other files.
Defines a prompt template that guides the LLM:
- System message β βTranslate the following from English into {language}.β
- User message β Inserts the actual
{text}provided by the user. - Returns a
ChatPromptTemplateready to be invoked by the model.
Handles the chat/translation logic:
-
translateText(language, text)β Takes input and returns a full translated response. -
translateStream(language, text)β Streams the translation token by token, like live typing.
The entry point of the project.
- Calls
translateTextto get a complete translation. - Calls
translateStreamto demonstrate streaming mode.
Run with:
node index.jsFinal: Ciao, come stai?
Streaming:
Β‘Buenos dΓas, mi amigo!
- β Interactive CLI chatbot (type input, get responses in real time).
- π REST API endpoint for frontend integration.
- πΎ Conversation memory with LangChain.
- π€ Speech-to-text and text-to-speech support.
- Fork the repo
- Create a feature branch (
git checkout -b feature/my-feature) - Commit changes (
git commit -m "Added my feature") - Push to branch (
git push origin feature/my-feature) - Open a Pull Request
This project is licensed under the MIT License.