Spaces:
Build error
Build error
Update mistral_api.py
Browse files- mistral_api.py +33 -23
mistral_api.py
CHANGED
|
@@ -26,28 +26,38 @@ client = InferenceClient("mistralai/Mistral-Small-24B-Instruct-2501")#, token=os
|
|
| 26 |
metaprompt_filepath = "meta_prompt.txt"
|
| 27 |
META_PROMPT = load_file(metaprompt_filepath)
|
| 28 |
|
| 29 |
-
def get_response(
|
| 30 |
-
"""Chat completion using HuggingFace's Inference API"""
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
print("Got to get_response")
|
| 34 |
-
# uncomment the lines below and add a parameter history: list[tuple[str, str]] to support history
|
| 35 |
-
#if history:
|
| 36 |
-
# for val in history:
|
| 37 |
-
# if val[0]:
|
| 38 |
-
# messages.append({"role":"user", "content":val[0]})
|
| 39 |
-
# if val[1]:
|
| 40 |
-
# messages.append({"role": "assistant", "content": val[1]})
|
| 41 |
-
|
| 42 |
-
messages.append({"role": "user", "content": message})
|
| 43 |
-
response = ""
|
| 44 |
-
for message in client.chat_completion(
|
| 45 |
-
messages,
|
| 46 |
max_tokens=max_tokens,
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
| 26 |
metaprompt_filepath = "meta_prompt.txt"
|
| 27 |
META_PROMPT = load_file(metaprompt_filepath)
|
| 28 |
|
| 29 |
+
def get_response(messages: list[dict], temperature=0, max_tokens=800) -> str:
|
| 30 |
+
"""Chat completion using HuggingFace's Inference API."""
|
| 31 |
+
response = client.chat_completion(
|
| 32 |
+
messages=messages,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
max_tokens=max_tokens,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
stream=True # Enables streaming
|
| 36 |
+
)
|
| 37 |
+
return response.["choices"][0]["delta"]["content"]
|
| 38 |
+
|
| 39 |
+
# def get_response(message, system_message=META_PROMPT, max_tokens=800, temperature=0) -> str:
|
| 40 |
+
# """Chat completion using HuggingFace's Inference API"""
|
| 41 |
+
|
| 42 |
+
# messages = [{"role":"system", "content":system_message}]
|
| 43 |
+
# print("Got to get_response")
|
| 44 |
+
# # uncomment the lines below and add a parameter history: list[tuple[str, str]] to support history
|
| 45 |
+
# #if history:
|
| 46 |
+
# # for val in history:
|
| 47 |
+
# # if val[0]:
|
| 48 |
+
# # messages.append({"role":"user", "content":val[0]})
|
| 49 |
+
# # if val[1]:
|
| 50 |
+
# # messages.append({"role": "assistant", "content": val[1]})
|
| 51 |
+
|
| 52 |
+
# messages.append({"role": "user", "content": message})
|
| 53 |
+
# response = ""
|
| 54 |
+
# for message in client.chat_completion(
|
| 55 |
+
# messages,
|
| 56 |
+
# max_tokens=max_tokens,
|
| 57 |
+
# stream=True,
|
| 58 |
+
# temperature=temperature
|
| 59 |
+
# ):
|
| 60 |
+
# token = message.choices[0].delta.content
|
| 61 |
+
# response += str(token)
|
| 62 |
|
| 63 |
+
# yield response
|