Haste171 / langchain-chatbot

AI Chatbot for analyzing/extracting information from data in conversational format.
MIT License
399 stars 88 forks source link

Sweep: Convert Langchain Chatbot to an API #45

Open Haste171 opened 8 months ago

Haste171 commented 8 months ago

Instead of using a Discord bot to allow connection to the langchain chatbot, rewrite the code to use FastAPI to do so.

Checklist - [X] Create `app/main.py` ✓ https://github.com/Haste171/langchain-chatbot/commit/b7ea58b44f72e2997d52f5957d223c80301d9903 [Edit](https://github.com/Haste171/langchain-chatbot/edit/sweep/convert_langchain_chatbot_to_an_api/app/main.py) - [X] Running GitHub Actions for `app/main.py` ✓ [Edit](https://github.com/Haste171/langchain-chatbot/edit/sweep/convert_langchain_chatbot_to_an_api/app/main.py) - [X] Modify `README.md` ✓ https://github.com/Haste171/langchain-chatbot/commit/58e5d481032dce6466df6b14bef20ac93cf48f41 [Edit](https://github.com/Haste171/langchain-chatbot/edit/sweep/convert_langchain_chatbot_to_an_api/README.md#L23-L59) - [X] Running GitHub Actions for `README.md` ✓ [Edit](https://github.com/Haste171/langchain-chatbot/edit/sweep/convert_langchain_chatbot_to_an_api/README.md#L23-L59) - [X] Modify `deprecated/chatbot.py` ✓ https://github.com/Haste171/langchain-chatbot/commit/241f9d3a4eb32a6b0f09f99d8c05d670b3de0e97 [Edit](https://github.com/Haste171/langchain-chatbot/edit/sweep/convert_langchain_chatbot_to_an_api/deprecated/chatbot.py) - [X] Running GitHub Actions for `deprecated/chatbot.py` ✓ [Edit](https://github.com/Haste171/langchain-chatbot/edit/sweep/convert_langchain_chatbot_to_an_api/deprecated/chatbot.py)
sweep-ai[bot] commented 8 months ago

🚀 Here's the PR! #46

See Sweep's progress at the progress dashboard!
Sweep Basic Tier: I'm using GPT-4. You have 5 GPT-4 tickets left for the month and 3 for the day. (tracking ID: b7d98b7361)

For more GPT-4 tickets, visit our payment portal. For a one week free trial, try Sweep Pro (unlimited GPT-4 tickets).
Install Sweep Configs: Pull Request

Actions (click)

Sandbox Execution ✓

Here are the sandbox execution logs prior to making any changes:

Sandbox logs for 5f913d0
Checking README.md for syntax errors... ✅ README.md has no syntax errors! 1/1 ✓
Checking README.md for syntax errors...
✅ README.md has no syntax errors!

Sandbox passed on the latest main, so sandbox checks will be enabled for this issue.


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/Haste171/langchain-chatbot/blob/5f913d074827946f318181c11e17229846441352/README.md#L15-L74 https://github.com/Haste171/langchain-chatbot/blob/main/utils/llm_query.py#L20-L55 https://github.com/Haste171/langchain-chatbot/blob/5f913d074827946f318181c11e17229846441352/deprecated/chatbot.py#L1-L128

Step 2: ⌨️ Coding

Ran GitHub Actions for b7ea58b44f72e2997d52f5957d223c80301d9903:

--- 
+++ 
@@ -39,7 +39,15 @@
 ### Setup
 ```
 git clone https://github.com/Haste171/langchain-chatbot.git
+cd langchain-chatbot
+pip install -r requirements.txt
 ```
+### Run the FastAPI Application
+Ensure you have installed FastAPI and Uvicorn, then run the following commands:
+```
+uvicorn app.main:app --reload
+```
+This will start the FastAPI server, making the Langchain Chatbot API available at `http://localhost:8000` where you can send request to the endpoints specified.

 Reference [example.env](https://github.com/Haste171/langchain-chatbot/blob/main/example.env) to create `.env` file
 ```python
@@ -56,7 +64,10 @@
 docker run -d langchain-chatbot
 ```

-# 🔧 Key Features
+# 
+
+✅ FastAPI based Web API Integration
+✅ Ingest and Query through RESTful Endpoints🔧 Key Features

 ✅ Credential Manager (OpenAI Keys)

@@ -92,4 +103,35 @@
 ![fixed-prev](https://cdn.discordapp.com/attachments/1114412425115086888/1114420571833376768/image.png)
 ![fixed-prev](https://cdn.discordapp.com/attachments/1114412425115086888/1114421482429354065/image.png)

-Maintained by Developers of [legalyze.ai](https://legalyze.ai)+Maintained by Developers of [legalyze.ai](https://legalyze.ai)
+
+# 📄 API Usage
+Here's how you can interact with the API:
+
+### Ingest Data
+Send a POST request to `/ingest` to ingest data:
+```python
+import requests
+response = requests.post('http://localhost:8000/ingest')
+print(response.json())
+```
+Expect a confirmation response that the data has been ingested.
+
+### Query Data
+Send a GET request to `/query/{question}` with your question:
+```python
+import requests
+question = 'What is the answer to my question?'
+response = requests.get(f'http://localhost:8000/query/{question}')
+print(response.json())
+```
+
+### Chat History
+Retrieve the chat history by sending a GET request to `/chat_history`:
+```python
+import requests
+response = requests.get('http://localhost:8000/chat_history')
+print(response.json())
+```
+
+These endpoints will allow you to ingest, query, and view the history of interactions with the Langchain Chatbot API.

Ran GitHub Actions for 58e5d481032dce6466df6b14bef20ac93cf48f41:

--- 
+++ 
@@ -1,13 +1,9 @@
 # Import necessary modules
-from colorama import init, Fore, Style
 import os
 import json
 from utils.ingest import ingest
 from utils.query import query
 from dotenv import load_dotenv
-
-# Initialize colorama module
-init()

 # Load environment variables
 load_dotenv()
@@ -21,53 +17,8 @@
 temperature = 0.7
 source_amount = 4

-# Print the startup display
-print(f"""{Fore.WHITE}Using the following credentials:{Fore.WHITE}
-OpenAI API Key: {Fore.RED}{openai_api_key}{Fore.WHITE}
-Pinecone API Key: {Fore.BLUE}{pinecone_api_key}{Fore.WHITE}
-Pinecone Environment: {Fore.BLUE}{pinecone_environment}{Fore.WHITE}
-Pinecone Index: {Fore.BLUE}{pinecone_index}{Fore.WHITE}
-Pinecone Namespace: {Fore.GREEN}{pinecone_namespace}{Fore.WHITE}
-
-{Fore.WHITE}Using the following settings:{Fore.WHITE}
-Temperature (Creativity): {Fore.MAGENTA}{temperature}{Fore.WHITE}
-Sources (Cites): {Fore.MAGENTA}{source_amount}{Fore.WHITE}
-""")
-
 # Check whether the user wants to use Pinecone
-r = input('Do you want to use Pinecone? (Y/N): ')
-if r == 'Y' and pinecone_api_key != '':
-    use_pinecone = True
-else:
-    print('Not using Pinecone or empty Pinecone API key provided. Using Chroma instead')
-    use_pinecone = False
-
-# Check whether the user wants to ingest
-r = input('Do you want to ingest? (Y/N): ')
-
-# If the user wants to ingest, call the ingest function
-if r == 'Y':
-    ingest_response = ingest(openai_api_key=openai_api_key, pinecone_api_key=pinecone_api_key,
-                             pinecone_environment=pinecone_environment, pinecone_index=pinecone_index,
-                             pinecone_namespace=pinecone_namespace, use_pinecone=use_pinecone)
-    print(ingest_response)
-
-# If the user doesn't want to ingest, check whether to use Pinecone or Chroma
-elif r == 'N':
-    if use_pinecone:
-        print('Using already ingested namespace at Pinecone.')
-    else:
-        print('Using already ingested vectors at ./vectorstore.')
-
-# If no method provided, skip
-else:
-    print('No method given, passing')
-    pass
-
-# Call the query function and set up a chat loop
-process = query(openai_api_key=openai_api_key, pinecone_api_key=pinecone_api_key,
-                pinecone_environment=pinecone_environment, pinecone_index=pinecone_index,
-                pinecone_namespace=pinecone_namespace, temperature=temperature, sources=source_amount, use_pinecone=use_pinecone)
+use_pinecone = True if pinecone_api_key else False

 def chat_loop():
     # Set up chat history list

Ran GitHub Actions for 241f9d3a4eb32a6b0f09f99d8c05d670b3de0e97:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/convert_langchain_chatbot_to_an_api.


🎉 Latest improvements to Sweep:


💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. Join Our Discord