Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sweep: Convert Langchain Chatbot to an API #45

Open
6 tasks done
Haste171 opened this issue Dec 19, 2023 · 1 comment · May be fixed by #46
Open
6 tasks done

Sweep: Convert Langchain Chatbot to an API #45

Haste171 opened this issue Dec 19, 2023 · 1 comment · May be fixed by #46
Labels

Comments

@Haste171
Copy link
Owner

Haste171 commented Dec 19, 2023

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

Checklist
  • Create app/main.pyb7ea58b Edit
  • Running GitHub Actions for app/main.pyEdit
  • Modify README.md58e5d48 Edit
  • Running GitHub Actions for README.mdEdit
  • Modify deprecated/chatbot.py241f9d3 Edit
  • Running GitHub Actions for deprecated/chatbot.pyEdit
@Haste171 Haste171 added the sweep label Dec 19, 2023
Copy link

sweep-ai bot commented Dec 19, 2023

🚀 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)

  • ↻ Restart Sweep

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.

<a href="https://discord.gg/KgmN4FPxxT"><img src="https://dcbadge.vercel.app/api/server/KgmN4FPxxT?compact=true&style=flat"></a>
</a>
<!-- *The LangChain Chatbot is an AI chat interface for the open-source library LangChain. It provides conversational answers to questions about vector ingested documents.* -->
<!-- *Existing repo development is at a freeze while we develop a langchain chat bot website :)* -->
# 🚀 Installation
## User-Setup
You can either join the [Discord](https://discord.gg/8vzXR9MGyc) server to use the bot or invite the [Langchain Chatbot](https://discord.com/api/oauth2/authorize?client_id=1113492778899476533&permissions=8&scope=bot) to your own server.
*If not you can following to steps below to setup your own Langchain Chatbot*
## Dev-Setup
Prerequisites:
- [Git](https://git-scm.com/downloads) - Free
- [Docker](https://www.docker.com/products/docker-desktop/) - Free
- [Discord Bot](https://discord.com/developers/applications) - Free
- [Mongo Database](https://youtu.be/dnEfQhjZgw0?t=326) - Free
- [Pinecone Database](https://youtu.be/tp0bQNDtLPc?t=48) - Free
- [OpenAI API Key](https://platform.openai.com/account/api-keys) - Billing Required
### Setup
```
git clone https://github.com/Haste171/langchain-chatbot.git
```
Reference [example.env](https://github.com/Haste171/langchain-chatbot/blob/main/example.env) to create `.env` file
```python
BOT_TOKEN=
MONGO_URI=
PINECONE_API_KEY=
PINECONE_INDEX=
PINECONE_ENV=
```
*Recommended to use a Docker Container for Deployment*
```
docker build -t langchain-chatbot .
docker run -d langchain-chatbot
```
# 🔧 Key Features
✅ Credential Manager (OpenAI Keys)
✅ Space Manager (Ingestions)
✅ Documentation Ingester (For readthedocs.io sites)
Soon:
- Compatibility with multiple files types (Llama Index)
- Compatibility with offline models (HuggingFace, Vicuna, Alpaca)
- Re-adding PDF Ingester Will be implemented along with docx, doc, excel, etc.
# 💻 Contributing

https://github.com/Haste171/langchain-chatbot/blob/main/utils/llm_query.py#L20-L55

# 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()
# Get API keys and settings from environment variables
openai_api_key = os.environ.get('OPENAI_API_KEY')
pinecone_api_key = os.environ.get('PINECONE_API_KEY')
pinecone_environment = os.environ.get('PINECONE_ENVIRONMENT')
pinecone_index = os.environ.get('PINECONE_INDEX')
pinecone_namespace = 'testing-pdf-0001'
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)
def chat_loop():
# Set up chat history list
chat_history = []
while True:
# Ask for user input
query = input("Please enter your question (or type 'exit' to end): ")
# If user wants to exit, break out of the loop
if query.lower() == 'exit':
break
# Get the response from the query function
result = process({"question": query, "chat_history": chat_history})
source_documents = result['source_documents']
# Parse the source documents for display
parsed_documents = []
for doc in source_documents:
parsed_doc = {
"page_content": doc.page_content,
"metadata": {
"author": doc.metadata.get("author", ""),
"creationDate": doc.metadata.get("creationDate", ""),
"creator": doc.metadata.get("creator", ""),
"file_path": doc.metadata.get("file_path", ""),
"format": doc.metadata.get("format", ""),
"keywords": doc.metadata.get("keywords", ""),
"modDate": doc.metadata.get("modDate", ""),
"page_number": doc.metadata.get("page_number", 0),
"producer": doc.metadata.get("producer", ""),
"source": doc.metadata.get("source", ""),
"subject": doc.metadata.get("subject", ""),
"title": doc.metadata.get("title", ""),
"total_pages": doc.metadata.get("total_pages", 0),
"trapped": doc.metadata.get("trapped", "")
}
}
parsed_documents.append(parsed_doc)
# Print the AI's answer
print(f'{Fore.BLUE}{Style.BRIGHT}AI:{Fore.RESET}{Style.NORMAL} {result["answer"]}')
# Append the user input and AI's answer to the chat history
chat_history.append((query, result["answer"]))
# Print the answer citations
print(f'\n{Fore.RED}Answer Citations')
for doc in parsed_documents:
print(f"{Fore.GREEN}{Style.BRIGHT}Source:{Fore.RESET}",
doc["metadata"]["source"])
print(f"{Fore.MAGENTA}Page Number:{Fore.RESET}",
doc["metadata"]["page_number"], f"{Style.NORMAL}")
# Write chat history to a JSON file
with open('chat_history.json', 'w') as json_file:
json.dump(chat_history, json_file, ensure_ascii=False, indent=4)


Step 2: ⌨️ Coding

Create app/main.py with contents:
• Create a new FastAPI application in the `app/main.py` file.
• Import the necessary modules, including FastAPI from the fastapi module, and the `llm_query` function from the `utils/llm_query.py` file.
• Define the FastAPI app with `app = FastAPI()`.
• Set up the necessary routes for the API. This will include a POST route for ingesting data, a GET route for querying data, and a GET route for retrieving the chat history.
• Within each route, call the appropriate functions from the `deprecated/chatbot.py` file to perform the necessary actions. For example, in the POST route for ingesting data, call the `ingest` function.
• Ensure that each route returns a JSON response with the appropriate data.
  • Running GitHub Actions for app/main.pyEdit
Check app/main.py with contents:

Ran GitHub Actions for b7ea58b44f72e2997d52f5957d223c80301d9903:

Modify README.md with contents:
• Update the Installation section to include instructions on how to run the new FastAPI application. This should include instructions on how to install FastAPI and any other necessary dependencies, as well as how to start the application.
• Update the Key Features section to reflect the new features of the application, including the new API endpoints.
• Add a new section on how to use the API, including examples of how to make requests to each endpoint and what data to expect in the response.
--- 
+++ 
@@ -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.
  • Running GitHub Actions for README.mdEdit
Check README.md with contents:

Ran GitHub Actions for 58e5d481032dce6466df6b14bef20ac93cf48f41:

Modify deprecated/chatbot.py with contents:
• Refactor the code in the `deprecated/chatbot.py` file to be used as functions in the new FastAPI application.
• Remove any code related to the Discord bot, as this will no longer be used.
• Update the `ingest` and `query` functions to accept parameters from the API requests and return data in a format that can be returned as a JSON response.
--- 
+++ 
@@ -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
  • Running GitHub Actions for deprecated/chatbot.pyEdit
Check deprecated/chatbot.py with contents:

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:

  • We just released a dashboard to track Sweep's progress on your issue in real-time, showing every stage of the process – from search to planning and coding.
  • Sweep uses OpenAI's latest Assistant API to plan code changes and modify code! This is 3x faster and significantly more reliable as it allows Sweep to edit code and validate the changes in tight iterations, the same way as a human would.
  • Try using the GitHub issues extension to create Sweep issues directly from your editor! GitHub Issues and Pull Requests.

💡 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

@sweep-ai sweep-ai bot linked a pull request Dec 19, 2023 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant