This folder contains a basic example demonstrating how to use LangChain with OpenAI's chat models without Retrieval Augmented Generation (RAG).
The example shows how to:
- Set up a basic chat interaction with OpenAI's models through LangChain
- Load environment variables securely
- Create and invoke a chat model
- Handle and display model responses
chat_model_basic.py
: Basic implementation of chat functionality using LangChain and OpenAI
- Python 3.x
- Required packages:
langchain
langchain-openai
python-dotenv
- Install the required packages:
pip install langchain langchain-openai python-dotenv
- Create a
.env
file in the root directory with your OpenAI API key:
OPENAI_API_KEY=your_api_key_here
Run the example script:
python chat_model_basic.py
The script will:
- Load your OpenAI API key from the environment
- Initialize a chat model (gpt-4o-mini)
- Send a query about Retrieval Augmented Generation in Cybersecurity
- Display both the full response object and its content
- The example uses the "gpt-4o-mini" model, but you can modify this to use other OpenAI models
- Make sure to keep your API key secure and never commit it to version control
- This is a basic example without RAG capabilities - see other examples for more advanced features
chat_model_basic.py
: Basic implementation of chat functionality using LangChain and OpenAIembeddings.py
: Demonstrates how to generate text embeddings using OpenAI's embedding models
Basic chat interaction with OpenAI models through LangChain.
The embeddings.py
script shows how to:
- Generate embeddings for text input using OpenAI's API
- Use the "text-embedding-3-small" model
- Process and display embedding vectors
Example usage:
python embeddings.py
The chatbot example includes a Streamlit web interface that provides:
- A clean, interactive chat interface
- Real-time streaming responses
- Persistent chat history within the session
- Easy-to-use input field for questions
To run the Streamlit interface:
streamlit run chatbot_example.py
- API keys are loaded from environment variables using python-dotenv
- Never hardcode API keys in your source code
- Add
.env
to your.gitignore
file
- Regularly rotate your API keys
- Monitor API usage for unusual patterns
- Implement rate limiting for production deployments
- Validate and sanitize user inputs
chat_model_basic.py
: Basic implementation of chat functionality using LangChain and OpenAIembeddings.py
: Demonstrates how to generate text embeddings using OpenAI's embedding modelschatbot_example.py
: Streamlit interface for the chatbot example.env
: Environment variables file for API keysrequirements.txt
: List of required packages