-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (67 loc) · 3.16 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import requests
from groq import Groq
from dotenv import load_dotenv
import streamlit as st
load_dotenv()
st.title('🦜🔗 Quickstart App')
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
class ChatAgent:
def __init__(self, api_key, model_name, verbose=False):
self.client = Groq(api_key=api_key)
self.model_name = model_name
self.conversation_history = []
self.verbose = verbose
def web_search(self, query):
subscription_key = os.environ.get("TAVILY_API_KEY")
search_url = "https://api.tavily.com/v1/search"
headers = {"Authorization": f"Bearer {subscription_key}"}
params = {"query": query, "textDecorations": True, "textFormat": "HTML"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()
return search_results["webPages"]["value"][0]["snippet"]
def get_response(self, user_input):
if self.verbose:
print(f"User input received: {user_input}")
print("Appending user input to conversation history...")
self.conversation_history.append({"role": "user", "content": user_input})
# Check if the user input indicates a need for real-time information
if "search" in user_input.lower():
if self.verbose:
print("Detected need for real-time information. Performing web search...")
search_query = user_input.lower().replace("search", "").strip()
web_search_result = self.web_search(search_query)
self.conversation_history.append({"role": "assistant", "content": web_search_result})
if self.verbose:
print(f"Web search result: {web_search_result}")
return web_search_result
if self.verbose:
print("Sending conversation history to Groq API for response...")
chat_completion = self.client.chat.completions.create(
messages=self.conversation_history,
model=self.model_name,
)
response = chat_completion.choices[0].message.content
if self.verbose:
print(f"Response received: {response}")
print("Appending assistant response to conversation history...")
self.conversation_history.append({"role": "assistant", "content": response})
if self.verbose:
print("Returning the response to the user.")
return response
def generate_response(user_input):
return agent.get_response(user_input)
# Set up the agent with your Groq API key and LLaMA model name
api_key = os.environ.get("GROQ_API_KEY")
model_name = "llama3-8b-8192"
agent = ChatAgent(api_key, model_name, verbose=True)
st.text_input("Type your message here:")
with st.form('my_form'):
text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?')
submitted = st.form_submit_button('Submit')
if not api_key:
st.warning('Please enter your Groq API key in the .env file!', icon='⚠')
if submitted and api_key:
response = generate_response(text)
st.write(response)