From 448f1078920f6eed791327f4fceb74dc11a0ae24 Mon Sep 17 00:00:00 2001 From: Aman Kumar <62813940+king04aman@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:27:58 +0530 Subject: [PATCH] add: custom GPT --- Custom GPT/README.md | 66 ++++++++++++++++++++++++++++++++++++++++ Custom GPT/data/data.txt | 0 Custom GPT/example-env | 1 + Custom GPT/main.py | 45 +++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 Custom GPT/README.md create mode 100644 Custom GPT/data/data.txt create mode 100644 Custom GPT/example-env create mode 100644 Custom GPT/main.py diff --git a/Custom GPT/README.md b/Custom GPT/README.md new file mode 100644 index 0000000..a69aaf0 --- /dev/null +++ b/Custom GPT/README.md @@ -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! diff --git a/Custom GPT/data/data.txt b/Custom GPT/data/data.txt new file mode 100644 index 0000000..e69de29 diff --git a/Custom GPT/example-env b/Custom GPT/example-env new file mode 100644 index 0000000..1c56e72 --- /dev/null +++ b/Custom GPT/example-env @@ -0,0 +1 @@ +OPENAI_API_KEY = "your_api_key" \ No newline at end of file diff --git a/Custom GPT/main.py b/Custom GPT/main.py new file mode 100644 index 0000000..5def529 --- /dev/null +++ b/Custom GPT/main.py @@ -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