Skip to content

Commit

Permalink
Merge pull request #167 from khengyun/update_chatbot_using_api
Browse files Browse the repository at this point in the history
update using gemini api
  • Loading branch information
khengyun authored Jul 8, 2024
2 parents a130c88 + d1745f2 commit f2e5865
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 163 deletions.
2 changes: 2 additions & 0 deletions api/chatbot/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Replace 'YOUR_API_KEY' with your actual API key
API_KEY = 'AIzaSyDTew5q4sTuY3JAmwhVotrxEEFqmp8IeD4'
71 changes: 45 additions & 26 deletions api/chatbot/chatbot_rag.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import minsearch
import requests
import os
import json
from g4f.client import Client
import g4f

_providers = [
g4f.Provider.Aichat,
g4f.Provider.ChatBase,
g4f.Provider.Bing,
g4f.Provider.GptGo,
g4f.Provider.You,
g4f.Provider.Yqcloud,
]
import minsearch
from dotenv import load_dotenv

# Import necessary libraries

# Load the environment variables from the .env file
load_dotenv()

# Retrieve the API key from environment variables
api_key = os.getenv('API_KEY')

def llm(prompt):
client = Client()
print("Creating chat completion...")
chat_completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
ignored=["Ylokh", "GptGo", "AItianhu", "Aibn", "Myshell", "FreeGpt"],
stream=True
)

response = ""
print("Waiting for completion...")
for completion in chat_completion:
response += completion.choices[0].delta.content or ""

# Define the endpoint URL
url = f'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={api_key}'

# Define headers
headers = {
'Content-Type': 'application/json'
}

# Define the data to send
data = {
"contents": [
{
"parts": [
{"text": prompt}
]
}
]
}

# Make the POST request
response = requests.post(url, headers=headers, json=data)

# Print the response
if response.status_code == 200:
response_data = response.json()
text_content = response_data['candidates'][0]['content']['parts'][0]['text']
print(text_content)
return {"response": text_content}
else:
print(f"Error: {response.status_code}")
print(response.text)

return {"response": response}
return {"response": "response error"}

def read_json(file):
with open(file, 'rt', encoding='utf-8') as f_in: # Specify UTF-8 encoding
Expand Down
Loading

0 comments on commit f2e5865

Please sign in to comment.