RAG Chat bot library
npm install ragifytext-embedding-3-large model.
sh
npm i ragify
`
$3
This package provides two key functions:
- uploadFile(filePath): Uploads a PDF file, generates embeddings, and stores them in the selected vector database.
- askQuestion(query): Retrieves relevant information from the stored embeddings and uses an LLM to generate a response.
Currently, Pinecone and ChromaDB are the supported vector databases.
---
š Environment Variables
Before using the library, set up your .env file with the required credentials.
$3
`sh
DB_TYPE=pinecone
PINECONE_API_KEY=your_pinecone_api_key
PINECONE_INDEX_NAME=your_index_name
PINECONE_ENV=your_pinecone_environment
OPENAI_API_KEY=your_open_ai_api_key
OPENAI_MODEL=your_desired_model by default the 'gpt-4' model will be used
OPENAI_EMBEDDING_MODEL=your_desired_model by default the text-embedding-3-large will be used
`
$3
`sh
DB_TYPE=chroma
CHROMA_DB_URL=http://localhost:8000
COLLECTION_NAME=pdf_embeddings
OPENAI_API_KEY=your_open_ai_api_key
OPENAI_MODEL=your_desired_model by default the 'gpt-4' model will be used
`
---
š Usage
To run the CLI tool:
`sh
node cli.js
`
Follow the prompts to select a database and provide the necessary details.
Alternatively, you can use the functions in your Node.js project:
`javascript
import { uploadFile, askQuestion } from "ragify";
// Upload a PDF file
await uploadFile("./documents/example.pdf");
// Ask a question based on the document
const response = await askQuestion("What is the summary of the document?");
console.log(response);
`
---
š How It Works
1ļøā£ User selects a vector database (Pinecone/ChromaDB).
2ļøā£ User provides the necessary database details.
3ļøā£ PDF file is loaded and split into chunks using LangChain.
4ļøā£ Embeddings are generated using the OpenAI API.
5ļøā£ Embeddings are stored in the selected vector database.
6ļøā£ When a query is made, relevant embeddings are retrieved and passed through an LLM to generate a response.
---
š Debugging Tips
If embeddings are not being stored correctly in Pinecone:
$3
`sh
curl -X GET "https://api.pinecone.io/v1/whoami" -H "Api-Key: ${PINECONE_API_KEY}"
`
$3
`sh
curl -X GET "https://controller.${PINECONE_ENV}.pinecone.io/databases" -H "Api-Key: ${PINECONE_API_KEY}"
`
$3
Modify uploadFile() to inspect document chunks:
`javascript
console.log(allSplits[0]);
`
---
š¤ Contributing
Contributions are welcome! Feel free to submit issues and pull requests to improve this library.
---
new upgrades
LangChain Conversational QA Agent
A powerful conversational question-answering system built on LangGraph and LangChain that maintains conversation history and performs retrieval-augmented generation (RAG).
Features
- Persistent Conversation History: Maintains context across multiple queries
- Retrieval-Augmented Generation: Enhances responses with information from your data sources
- Customizable LLM Integration: Supports OpenAI models by default with easy configuration
- Stateful Execution: Preserves conversations between API calls
- Express API Support: Ready to integrate with Express for web applications
Installation
`bash
npm install langchain-conversational-qa
`
Environment Setup
Create a .env file with your OpenAI API key:
`
OPENAI_API_KEY=your_openai_api_key
OPENAI_MODEL=gpt-4 # Optional, defaults to gpt-4
`
Quick Start
$3
`javascript
import { ConversationalAgent } from 'langchain-conversational-qa';
// Create a conversational agent instance
const agent = new ConversationalAgent();
// Ask questions
const response = await agent.query("What is LangChain?");
console.log(response);
// Follow-up questions maintain context
const followUp = await agent.query("Can you give me an example?");
console.log(followUp);
`
$3
`javascript
// Create an agent with custom options
const customAgent = new ConversationalAgent({
model: "gpt-4-turbo",
apiKey: "your-openai-api-key",
minHistorySize: 15 // Keep at least 15 conversation turns
});
`
$3
`javascript
import express from 'express';
import { ConversationalAgent } from 'langchain-conversational-qa';
const app = express();
const PORT = process.env.PORT || 3000;
const agent = new ConversationalAgent();
app.use(express.json());
app.post('/api/query', async (req, res) => {
try {
const { query } = req.body;
const response = await agent.query(query);
res.json({ answer: response });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'An error occurred' });
}
});
app.post('/api/reset', async (req, res) => {
try {
await agent.resetConversation();
res.json({ status: 'Conversation history reset' });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to reset conversation' });
}
});
app.get('/api/history', async (req, res) => {
try {
const history = await agent.getConversationHistory();
res.json({ history });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Failed to retrieve conversation history' });
}
});
app.listen(PORT, () => {
console.log(Server running on port ${PORT});
});
`
Maintaining Conversation History
The agent automatically maintains conversation history across multiple queries as long as you're using the same agent instance. This is particularly important in server environments where you need to create the agent once, outside of your request handlers.
`javascript
// CORRECT: Create once at server startup
const agent = new ConversationalAgent();
// INCORRECT: Creating a new agent for each request will lose history
app.post('/api/query', async (req, res) => {
const agent = new ConversationalAgent(); // Don't do this!
// ...
});
`
API Reference
$3
#### Constructor
`javascript
new ConversationalAgent(options?)
`
Options:
- model: OpenAI model to use (default: "gpt-4" or value from OPENAI_MODEL env variable)
- apiKey: OpenAI API key (default: value from OPENAI_API_KEY env variable)
- minHistorySize: Minimum conversation turns to maintain (default: 10)
#### Methods
- query(question: string): Process a question and return the answer
- resetConversation(): Clear conversation history
- getConversationHistory(): Get the current conversation history
$3
Creates a stateful executor function that can be used for standalone processing.
`javascript
const executor = createExecutor();
const result = await executor("What is LangChain?");
`
How It Works
The library uses LangGraph to define a processing workflow:
1. initialize_history: Sets up the conversation history
2. retrieve: Fetches relevant context from your data sources
3. generate`: Creates a response using the LLM, context, and conversation history