Spaces:
Sleeping
Sleeping
Update chatbot_page.py
Browse files- chatbot_page.py +19 -5
chatbot_page.py
CHANGED
|
@@ -36,14 +36,28 @@ def chat_with_user(user_message, history):
|
|
| 36 |
|
| 37 |
# Function to end chat and extract keywords
|
| 38 |
def extract_keywords_from_conversation(history):
|
|
|
|
|
|
|
| 39 |
# Combine all user and assistant messages into a single string
|
| 40 |
conversation = "\n".join([f"User: {msg[0]}\nAssistant: {msg[1]}" for msg in history if msg[1]])
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
"extract about 5 keywords that would be most useful for searching Hugging Face repos to find the most relevant results for the user. "
|
| 44 |
-
"Return only the keywords as a comma-separated list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
-
keywords =
|
| 47 |
return keywords
|
| 48 |
|
| 49 |
with gr.Blocks() as chatbot_demo:
|
|
|
|
| 36 |
|
| 37 |
# Function to end chat and extract keywords
|
| 38 |
def extract_keywords_from_conversation(history):
|
| 39 |
+
from openai import OpenAI
|
| 40 |
+
client = OpenAI()
|
| 41 |
# Combine all user and assistant messages into a single string
|
| 42 |
conversation = "\n".join([f"User: {msg[0]}\nAssistant: {msg[1]}" for msg in history if msg[1]])
|
| 43 |
+
system_prompt = (
|
| 44 |
+
"You are an expert at helping users find open-source repos on Hugging Face. "
|
| 45 |
+
"Given a conversation, extract about 5 keywords that would be most useful for searching Hugging Face repos to find the most relevant results for the user. "
|
| 46 |
+
"Return only the keywords as a comma-separated list."
|
| 47 |
+
)
|
| 48 |
+
user_prompt = (
|
| 49 |
+
"Conversation:\n" + conversation + "\n\nExtract about 5 keywords for Hugging Face repo search."
|
| 50 |
+
)
|
| 51 |
+
response = client.chat.completions.create(
|
| 52 |
+
model="gpt-4o-mini",
|
| 53 |
+
messages=[
|
| 54 |
+
{"role": "system", "content": system_prompt},
|
| 55 |
+
{"role": "user", "content": user_prompt}
|
| 56 |
+
],
|
| 57 |
+
max_tokens=64,
|
| 58 |
+
temperature=0.3
|
| 59 |
)
|
| 60 |
+
keywords = response.choices[0].message.content.strip()
|
| 61 |
return keywords
|
| 62 |
|
| 63 |
with gr.Blocks() as chatbot_demo:
|