Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,68 +26,6 @@ client = Groq(
|
|
| 26 |
api_key=os.environ['GROQ_API_KEY'],
|
| 27 |
)
|
| 28 |
|
| 29 |
-
def get_time_date_tool():
|
| 30 |
-
owner_info = {
|
| 31 |
-
"date_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
-
}
|
| 33 |
-
return json.dumps(owner_info)
|
| 34 |
-
|
| 35 |
-
def run_conversation(user_prompt, messages, model_option, max_tokens):
|
| 36 |
-
tools = [
|
| 37 |
-
{
|
| 38 |
-
"type": "function",
|
| 39 |
-
"function": {
|
| 40 |
-
"name": "time_date",
|
| 41 |
-
"description": "The tool will return information about the time and date to the AI.",
|
| 42 |
-
"parameters": {},
|
| 43 |
-
},
|
| 44 |
-
}
|
| 45 |
-
]
|
| 46 |
-
response = client.chat.completions.create(
|
| 47 |
-
model=model_option,
|
| 48 |
-
messages=[{"role": m["role"], "content": str(m["content"])} for m in messages],
|
| 49 |
-
tools=tools,
|
| 50 |
-
tool_choice="auto",
|
| 51 |
-
max_tokens=max_tokens
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
response_message = response.choices[0].message
|
| 55 |
-
tool_calls = response_message.tool_calls
|
| 56 |
-
|
| 57 |
-
if tool_calls:
|
| 58 |
-
available_functions = {
|
| 59 |
-
"time_date": get_time_date_tool
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
messages.append(response_message)
|
| 63 |
-
|
| 64 |
-
for tool_call in tool_calls:
|
| 65 |
-
function_name = tool_call.function.name
|
| 66 |
-
function_to_call = available_functions[function_name]
|
| 67 |
-
function_args = json.loads(tool_call.function.arguments)
|
| 68 |
-
if function_name not in available_functions:
|
| 69 |
-
function_response = "invalid tool"
|
| 70 |
-
else:
|
| 71 |
-
function_response = function_to_call(**function_args)
|
| 72 |
-
messages.append(
|
| 73 |
-
{
|
| 74 |
-
"tool_call_id": tool_call.id,
|
| 75 |
-
"role": "tool",
|
| 76 |
-
"name": function_name,
|
| 77 |
-
"content": function_response,
|
| 78 |
-
}
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
second_response = client.chat.completions.create(
|
| 82 |
-
model=model_option,
|
| 83 |
-
messages=[{"role": m["role"], "content": str(m["content"])} for m in messages]
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
return second_response.choices[0].message.content
|
| 87 |
-
else:
|
| 88 |
-
return response_message.content
|
| 89 |
-
|
| 90 |
-
|
| 91 |
# Initialize chat history and selected model
|
| 92 |
if "messages" not in st.session_state:
|
| 93 |
st.session_state.messages = []
|
|
@@ -147,33 +85,45 @@ for message in st.session_state.messages:
|
|
| 147 |
else:
|
| 148 |
st.warning("Message object does not have the 'role' attribute.")
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
| 153 |
"""Yield chat response content from the Groq API response."""
|
| 154 |
for chunk in chat_completion:
|
| 155 |
if chunk.choices[0].delta.content:
|
| 156 |
yield chunk.choices[0].delta.content
|
| 157 |
|
| 158 |
-
|
| 159 |
if prompt := st.chat_input("Enter your prompt here..."):
|
| 160 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 161 |
|
| 162 |
-
with st.chat_message("user", avatar="🕺"):
|
| 163 |
st.markdown(prompt)
|
| 164 |
|
| 165 |
# Fetch response from Groq API
|
| 166 |
try:
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
| 172 |
)
|
| 173 |
|
| 174 |
-
#
|
| 175 |
with st.chat_message("assistant", avatar="🤖"):
|
| 176 |
-
|
| 177 |
-
|
| 178 |
except Exception as e:
|
| 179 |
st.error(e, icon="🚨")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
api_key=os.environ['GROQ_API_KEY'],
|
| 27 |
)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Initialize chat history and selected model
|
| 30 |
if "messages" not in st.session_state:
|
| 31 |
st.session_state.messages = []
|
|
|
|
| 85 |
else:
|
| 86 |
st.warning("Message object does not have the 'role' attribute.")
|
| 87 |
|
|
|
|
|
|
|
| 88 |
def generate_chat_responses(chat_completion) -> Generator[str, None, None]:
|
| 89 |
"""Yield chat response content from the Groq API response."""
|
| 90 |
for chunk in chat_completion:
|
| 91 |
if chunk.choices[0].delta.content:
|
| 92 |
yield chunk.choices[0].delta.content
|
| 93 |
|
|
|
|
| 94 |
if prompt := st.chat_input("Enter your prompt here..."):
|
| 95 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 96 |
|
| 97 |
+
with st.chat_message("user", avatar="🕺"):
|
| 98 |
st.markdown(prompt)
|
| 99 |
|
| 100 |
# Fetch response from Groq API
|
| 101 |
try:
|
| 102 |
+
chat_completion = client.chat.completions.create(
|
| 103 |
+
model=model_option,
|
| 104 |
+
messages=[
|
| 105 |
+
{"role": m["role"], "content": m["content"]}
|
| 106 |
+
for m in st.session_state.messages
|
| 107 |
+
],
|
| 108 |
+
max_tokens=max_tokens,
|
| 109 |
+
stream=True,
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# Use the generator function with st.write_stream
|
| 113 |
with st.chat_message("assistant", avatar="🤖"):
|
| 114 |
+
chat_responses_generator = generate_chat_responses(chat_completion)
|
| 115 |
+
full_response = st.write_stream(chat_responses_generator)
|
| 116 |
except Exception as e:
|
| 117 |
st.error(e, icon="🚨")
|
| 118 |
+
|
| 119 |
+
# Append the full response to session_state.messages
|
| 120 |
+
if isinstance(full_response, str):
|
| 121 |
+
st.session_state.messages.append(
|
| 122 |
+
{"role": "assistant", "content": full_response}
|
| 123 |
+
)
|
| 124 |
+
else:
|
| 125 |
+
# Handle the case where full_response is not a string
|
| 126 |
+
combined_response = "\n".join(str(item) for item in full_response)
|
| 127 |
+
st.session_state.messages.append(
|
| 128 |
+
{"role": "assistant", "content": combined_response}
|
| 129 |
+
)
|