Skip to content

sudarshantanwer/Langchain-Chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌐 LangChain Chatbot (Translator)

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

πŸ“‚ Project Structure

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

πŸš€ Installation

1. Clone the Repository

git clone <repo-url>
cd langchain-chatbot

2. Install Dependencies

npm install

Required packages:


πŸ”‘ Environment Variables

Setup

Copy the example file and edit with your own keys:

cp .env.example .env

.env.example

# 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

⚠️ Never commit .env to git.


πŸ“œ File Explanations

1. src/model.js

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.

2. src/prompt.js

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 ChatPromptTemplate ready to be invoked by the model.

3. src/chat.js

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.


4. index.js

The entry point of the project.

  • Calls translateText to get a complete translation.
  • Calls translateStream to demonstrate streaming mode.

Run with:

node index.js

πŸ–₯️ Example Output

Final: Ciao, come stai?

Streaming:
Β‘Buenos dΓ­as, mi amigo!

πŸ› οΈ Upcoming Improvements

  • βœ… 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.

🀝 Contributing

  1. Fork the repo
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Commit changes (git commit -m "Added my feature")
  4. Push to branch (git push origin feature/my-feature)
  5. Open a Pull Request

πŸ“œ License

This project is licensed under the MIT License.

About

🌐 LangChain Chatbot (Translator) A simple yet powerful chatbot built with LangChain and OpenAI in JavaScript. It translates English text into multiple languages using prompt templates and LLM calls. Includes support for both standard responses and streaming mode, with optional LangSmith tracing for debugging and monitoring.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors