-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97f9f26
commit 448f107
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Conversational Retrieval with LangChain and OpenAI | ||
|
||
This directory contains a Python script that implements a conversational retrieval system using LangChain and OpenAI's API. The script allows users to query a collection of documents and receive responses based on the retrieved information. | ||
|
||
## Features | ||
|
||
- Load documents from a specified directory. | ||
- Create and persist a vector store index for efficient querying. | ||
- Engage in conversational interactions, maintaining chat history. | ||
- Easily exit the program. | ||
|
||
## Requirements | ||
|
||
- Python 3.7+ | ||
- Required packages: | ||
- `openai` | ||
- `langchain` | ||
- `chromadb` | ||
|
||
You can install the required packages using pip: | ||
|
||
```bash | ||
pip install openai langchain chromadb | ||
``` | ||
## Setup | ||
1. Clone the Repository: | ||
```bash | ||
git clone https://github.com/king04aman/custom-gpt.git | ||
cd your_repository | ||
``` | ||
2. Set the OpenAI API Key: | ||
Replace `your_api_key_here` in the script with your actual OpenAI API key. You can also set the environment variable directly in your terminal: | ||
```bash | ||
export OPENAI_API_KEY="your_api_key_here" | ||
``` | ||
3. Prepare Your Data: | ||
Place your documents in a folder named `data/`. The script will load all documents from this directory. | ||
|
||
## Usage | ||
Run the script from the command line: | ||
```bash | ||
python main.py | ||
``` | ||
### Command Line Arguments | ||
You can provide an initial query as a command line argument: | ||
```bash | ||
python main.py "Your initial query here" | ||
``` | ||
### Interactive Mode | ||
If no initial query is provided, the script will prompt you to enter queries interactively. Type your question and press Enter to get a response. Type `quit`, `q`, or exit to `exit` the program. | ||
|
||
### Persistence | ||
- Set the `PERSIST` variable to `True` in the script to enable saving the vector store index to disk for reuse in future sessions. | ||
- The index will be saved in a directory named `persist/`. | ||
|
||
## Example | ||
```bash | ||
Prompt (type 'quit' to exit): What is the significance of data persistence? | ||
Response: [Your response here based on the documents] | ||
``` | ||
|
||
## License | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. | ||
|
||
## Contributing | ||
Feel free to submit issues or pull requests. Contributions are welcome! |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OPENAI_API_KEY = "your_api_key" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import openai | ||
import sys | ||
from langchain.chains import ConversationalRetrievalChain | ||
from langchain.chat_models import ChatOpenAI | ||
from langchain.document_loaders import DirectoryLoader | ||
from langchain.embeddings import OpenAIEmbeddings | ||
from langchain.indexes import VectorstoreIndexCreator | ||
from langchain.indexes.vectorstore import VectorStoreIndexWrapper | ||
from langchain.vectorstores import Chroma | ||
|
||
|
||
os.environ["OPENAI_API_KEY"] = "your_api_key_here" | ||
PERSIST = False | ||
|
||
query = sys.argv[1] if len(sys.argv) > 1 else None | ||
|
||
if PERSIST and os.path.exists("persist"): | ||
print("Reusing index...\n") | ||
vectorstore = Chroma(persist_directory="persist", embedding_function=OpenAIEmbeddings()) | ||
index = VectorStoreIndexWrapper(vectorstore=vectorstore) | ||
else: | ||
loader = DirectoryLoader("data/") | ||
index = VectorstoreIndexCreator(vectorstore_kwargs={"persist_directory": "persist"}).from_loaders([loader]) if PERSIST else VectorstoreIndexCreator().from_loaders([loader]) | ||
|
||
chain = ConversationalRetrievalChain.from_llm( | ||
llm=ChatOpenAI(model="gpt-3.5-turbo"), | ||
retriever=index.vectorstore.as_retriever(search_kwargs={"k": 1}), | ||
) | ||
|
||
chat_history = [] | ||
|
||
while True: | ||
if not query: | ||
query = input("Prompt (type 'quit' to exit): ") | ||
if query.lower() in ['quit', 'q', 'exit']: | ||
print("Exiting the program...") | ||
sys.exit() | ||
|
||
result = chain({"question": query, "chat_history": chat_history}) | ||
|
||
print("Response:", result['answer']) | ||
|
||
chat_history.append((query, result['answer'])) | ||
query = None |