Spaces:
Sleeping
Sleeping
Update excel_chat.py
Browse files- excel_chat.py +48 -58
excel_chat.py
CHANGED
|
@@ -10,76 +10,66 @@ from users_management import update_json, users
|
|
| 10 |
|
| 11 |
#users = ['maksG', 'AlmaA', 'YchK']
|
| 12 |
|
| 13 |
-
def ask_llm(query, input, client_index
|
| 14 |
messages = [
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
{
|
| 20 |
-
"role": "user",
|
| 21 |
-
"content": f"{input}",
|
| 22 |
-
}
|
| 23 |
-
]
|
| 24 |
-
systemC = f"You are a helpful assistant. Only show your final response to the **User Query**! Do not provide any explanations or details: \n# User Query:\n{query}."
|
| 25 |
-
messageC=[
|
| 26 |
{
|
| 27 |
"role": "user",
|
| 28 |
-
"content":
|
| 29 |
-
{
|
| 30 |
-
"type": "text",
|
| 31 |
-
"text": f"{input}"
|
| 32 |
-
}
|
| 33 |
-
]
|
| 34 |
}
|
| 35 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
if
|
| 38 |
-
client =
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
model=
|
| 54 |
-
)
|
| 55 |
-
elif client_index == "Mistral Medium":
|
| 56 |
-
client = MistralClient(api_key=os.environ[user['api_keys']['mistral']])
|
| 57 |
-
chat_completion = client.chat(
|
| 58 |
-
messages=messages,
|
| 59 |
-
model='mistral-medium',
|
| 60 |
-
)
|
| 61 |
-
elif client_index == "Claude Opus":
|
| 62 |
-
client = anthropic.Anthropic(api_key=os.environ[user['api_keys']['claude']])
|
| 63 |
-
chat_completion = client.messages.create(
|
| 64 |
-
model="claude-3-opus-20240229",
|
| 65 |
max_tokens=350,
|
| 66 |
temperature=0,
|
| 67 |
system=systemC,
|
| 68 |
messages=messageC
|
| 69 |
-
)
|
| 70 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
else:
|
| 72 |
-
client
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
messages=messageC
|
| 79 |
-
).content[0].text
|
| 80 |
-
return chat_completion
|
| 81 |
|
| 82 |
-
return chat_completion.choices[0].message.content
|
| 83 |
|
| 84 |
def filter_df(df, column_name, keywords):
|
| 85 |
if len(keywords)>0:
|
|
|
|
| 10 |
|
| 11 |
#users = ['maksG', 'AlmaA', 'YchK']
|
| 12 |
|
| 13 |
+
def ask_llm(query, input, client_index):
|
| 14 |
messages = [
|
| 15 |
+
{
|
| 16 |
+
"role": "system",
|
| 17 |
+
"content": f"You are a helpful assistant. Only show your final response to the **User Query**! Do not provide any explanations or details: \n# User Query:\n{query}."
|
| 18 |
+
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
{
|
| 20 |
"role": "user",
|
| 21 |
+
"content": input,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
]
|
| 24 |
+
|
| 25 |
+
systemC = messages[0]["content"]
|
| 26 |
+
messageC = [{
|
| 27 |
+
"role": "user",
|
| 28 |
+
"content": [{
|
| 29 |
+
"type": "text",
|
| 30 |
+
"text": input
|
| 31 |
+
}]
|
| 32 |
+
}]
|
| 33 |
|
| 34 |
+
if "Mistral" in client_index:
|
| 35 |
+
client = MistralClient(api_key=os.environ['MISTRAL_API_KEY'])
|
| 36 |
+
model_map = {
|
| 37 |
+
"Mistral Tiny": "mistral-tiny",
|
| 38 |
+
"Mistral Small": "mistral-small-latest",
|
| 39 |
+
"Mistral Medium": "mistral-medium",
|
| 40 |
+
}
|
| 41 |
+
chat_completion = client.chat(messages=messages, model=model_map[client_index])
|
| 42 |
+
|
| 43 |
+
elif "Claude" in client_index:
|
| 44 |
+
client = anthropic.Anthropic(api_key=os.environ['CLAUDE_API_KEY'])
|
| 45 |
+
model_map = {
|
| 46 |
+
"Claude Sonnet": "claude-3-sonnet-20240229",
|
| 47 |
+
"Claude Opus": "claude-3-opus-20240229",
|
| 48 |
+
}
|
| 49 |
+
response = client.messages.create(
|
| 50 |
+
model=model_map[client_index],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
max_tokens=350,
|
| 52 |
temperature=0,
|
| 53 |
system=systemC,
|
| 54 |
messages=messageC
|
| 55 |
+
)
|
| 56 |
+
return response.content[0].text
|
| 57 |
+
|
| 58 |
+
elif client_index == "Groq (mixtral)":
|
| 59 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 60 |
+
chat_completion = client.chat.completions.create(
|
| 61 |
+
messages=messages,
|
| 62 |
+
model='mixtral-8x7b-32768',
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
else:
|
| 66 |
+
raise ValueError("Unsupported client index provided")
|
| 67 |
+
|
| 68 |
+
# Return the response, handling the structure specific to Groq and Mistral clients.
|
| 69 |
+
return chat_completion.choices[0].message.content if client_index != "Claude" else chat_completion
|
| 70 |
+
|
| 71 |
+
|
|
|
|
|
|
|
|
|
|
| 72 |
|
|
|
|
| 73 |
|
| 74 |
def filter_df(df, column_name, keywords):
|
| 75 |
if len(keywords)>0:
|