RAMYASRI-39
commited on
Commit
•
dfae233
1
Parent(s):
62a1b90
Update backend/query_llm.py
Browse files- backend/query_llm.py +176 -175
backend/query_llm.py
CHANGED
@@ -1,175 +1,176 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import openai
|
4 |
-
import gradio as gr
|
5 |
-
|
6 |
-
from os import getenv
|
7 |
-
from typing import Any, Dict, Generator, List
|
8 |
-
|
9 |
-
from huggingface_hub import InferenceClient
|
10 |
-
from transformers import AutoTokenizer
|
11 |
-
from gradio_client import Client
|
12 |
-
#tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
13 |
-
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
14 |
-
#tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x22B-Instruct-v0.1")
|
15 |
-
temperature = 0.5
|
16 |
-
top_p = 0.7
|
17 |
-
repetition_penalty = 1.2
|
18 |
-
|
19 |
-
OPENAI_KEY = getenv("OPENAI_API_KEY")
|
20 |
-
HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
|
21 |
-
|
22 |
-
# hf_client = InferenceClient(
|
23 |
-
# "mistralai/Mistral-7B-Instruct-v0.1",
|
24 |
-
# token=HF_TOKEN
|
25 |
-
# )
|
26 |
-
|
27 |
-
client = Client("Qwen/Qwen1.5-110B-Chat-demo")
|
28 |
-
hf_client
|
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 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
#return
|
119 |
-
return response[1][0][1]
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
'
|
147 |
-
'
|
148 |
-
'
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
import openai
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
from os import getenv
|
7 |
+
from typing import Any, Dict, Generator, List
|
8 |
+
|
9 |
+
from huggingface_hub import InferenceClient
|
10 |
+
from transformers import AutoTokenizer
|
11 |
+
from gradio_client import Client
|
12 |
+
#tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
14 |
+
#tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x22B-Instruct-v0.1")
|
15 |
+
temperature = 0.5
|
16 |
+
top_p = 0.7
|
17 |
+
repetition_penalty = 1.2
|
18 |
+
|
19 |
+
OPENAI_KEY = getenv("OPENAI_API_KEY")
|
20 |
+
HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
|
21 |
+
|
22 |
+
# hf_client = InferenceClient(
|
23 |
+
# "mistralai/Mistral-7B-Instruct-v0.1",
|
24 |
+
# token=HF_TOKEN
|
25 |
+
# )
|
26 |
+
|
27 |
+
client = Client("Qwen/Qwen1.5-110B-Chat-demo")
|
28 |
+
hf_client=''
|
29 |
+
# hf_client = InferenceClient(
|
30 |
+
# "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
31 |
+
# token=HF_TOKEN
|
32 |
+
# )
|
33 |
+
def format_prompt(message: str, api_kind: str):
|
34 |
+
"""
|
35 |
+
Formats the given message using a chat template.
|
36 |
+
|
37 |
+
Args:
|
38 |
+
message (str): The user message to be formatted.
|
39 |
+
|
40 |
+
Returns:
|
41 |
+
str: Formatted message after applying the chat template.
|
42 |
+
"""
|
43 |
+
|
44 |
+
# Create a list of message dictionaries with role and content
|
45 |
+
messages: List[Dict[str, Any]] = [{'role': 'user', 'content': message}]
|
46 |
+
|
47 |
+
if api_kind == "openai":
|
48 |
+
return messages
|
49 |
+
elif api_kind == "hf":
|
50 |
+
return tokenizer.apply_chat_template(messages, tokenize=False)
|
51 |
+
elif api_kind:
|
52 |
+
raise ValueError("API is not supported")
|
53 |
+
|
54 |
+
|
55 |
+
def generate_hf(prompt: str, history: str, temperature: float = 0.5, max_new_tokens: int = 4000,
|
56 |
+
top_p: float = 0.95, repetition_penalty: float = 1.0) -> Generator[str, None, str]:
|
57 |
+
"""
|
58 |
+
Generate a sequence of tokens based on a given prompt and history using Mistral client.
|
59 |
+
|
60 |
+
Args:
|
61 |
+
prompt (str): The initial prompt for the text generation.
|
62 |
+
history (str): Context or history for the text generation.
|
63 |
+
temperature (float, optional): The softmax temperature for sampling. Defaults to 0.9.
|
64 |
+
max_new_tokens (int, optional): Maximum number of tokens to be generated. Defaults to 256.
|
65 |
+
top_p (float, optional): Nucleus sampling probability. Defaults to 0.95.
|
66 |
+
repetition_penalty (float, optional): Penalty for repeated tokens. Defaults to 1.0.
|
67 |
+
|
68 |
+
Returns:
|
69 |
+
Generator[str, None, str]: A generator yielding chunks of generated text.
|
70 |
+
Returns a final string if an error occurs.
|
71 |
+
"""
|
72 |
+
|
73 |
+
temperature = max(float(temperature), 1e-2) # Ensure temperature isn't too low
|
74 |
+
top_p = float(top_p)
|
75 |
+
|
76 |
+
generate_kwargs = {
|
77 |
+
'temperature': temperature,
|
78 |
+
'max_new_tokens': max_new_tokens,
|
79 |
+
'top_p': top_p,
|
80 |
+
'repetition_penalty': repetition_penalty,
|
81 |
+
'do_sample': True,
|
82 |
+
'seed': 42,
|
83 |
+
}
|
84 |
+
|
85 |
+
formatted_prompt = format_prompt(prompt, "hf")
|
86 |
+
|
87 |
+
try:
|
88 |
+
stream = hf_client.text_generation(formatted_prompt, **generate_kwargs,
|
89 |
+
stream=True, details=True, return_full_text=False)
|
90 |
+
output = ""
|
91 |
+
for response in stream:
|
92 |
+
output += response.token.text
|
93 |
+
yield output
|
94 |
+
|
95 |
+
except Exception as e:
|
96 |
+
if "Too Many Requests" in str(e):
|
97 |
+
print("ERROR: Too many requests on Mistral client")
|
98 |
+
gr.Warning("Unfortunately Mistral is unable to process")
|
99 |
+
return "Unfortunately, I am not able to process your request now."
|
100 |
+
elif "Authorization header is invalid" in str(e):
|
101 |
+
print("Authetification error:", str(e))
|
102 |
+
gr.Warning("Authentication error: HF token was either not provided or incorrect")
|
103 |
+
return "Authentication error"
|
104 |
+
else:
|
105 |
+
print("Unhandled Exception:", str(e))
|
106 |
+
gr.Warning("Unfortunately Mistral is unable to process")
|
107 |
+
return "I do not know what happened, but I couldn't understand you."
|
108 |
+
|
109 |
+
def generate_qwen(formatted_prompt: str, history: str):
|
110 |
+
response = client.predict(
|
111 |
+
query=formatted_prompt,
|
112 |
+
history=[],
|
113 |
+
system='You are wonderful',
|
114 |
+
api_name="/model_chat"
|
115 |
+
)
|
116 |
+
print('Response:',response)
|
117 |
+
|
118 |
+
#return output
|
119 |
+
#return response[1][0][1]
|
120 |
+
return response[1][0][1]
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
def generate_openai(prompt: str, history: str, temperature: float = 0.9, max_new_tokens: int = 256,
|
125 |
+
top_p: float = 0.95, repetition_penalty: float = 1.0) -> Generator[str, None, str]:
|
126 |
+
"""
|
127 |
+
Generate a sequence of tokens based on a given prompt and history using Mistral client.
|
128 |
+
|
129 |
+
Args:
|
130 |
+
prompt (str): The initial prompt for the text generation.
|
131 |
+
history (str): Context or history for the text generation.
|
132 |
+
temperature (float, optional): The softmax temperature for sampling. Defaults to 0.9.
|
133 |
+
max_new_tokens (int, optional): Maximum number of tokens to be generated. Defaults to 256.
|
134 |
+
top_p (float, optional): Nucleus sampling probability. Defaults to 0.95.
|
135 |
+
repetition_penalty (float, optional): Penalty for repeated tokens. Defaults to 1.0.
|
136 |
+
|
137 |
+
Returns:
|
138 |
+
Generator[str, None, str]: A generator yielding chunks of generated text.
|
139 |
+
Returns a final string if an error occurs.
|
140 |
+
"""
|
141 |
+
|
142 |
+
temperature = max(float(temperature), 1e-2) # Ensure temperature isn't too low
|
143 |
+
top_p = float(top_p)
|
144 |
+
|
145 |
+
generate_kwargs = {
|
146 |
+
'temperature': temperature,
|
147 |
+
'max_tokens': max_new_tokens,
|
148 |
+
'top_p': top_p,
|
149 |
+
'frequency_penalty': max(-2., min(repetition_penalty, 2.)),
|
150 |
+
}
|
151 |
+
|
152 |
+
formatted_prompt = format_prompt(prompt, "openai")
|
153 |
+
|
154 |
+
try:
|
155 |
+
stream = openai.ChatCompletion.create(model="gpt-3.5-turbo-0301",
|
156 |
+
messages=formatted_prompt,
|
157 |
+
**generate_kwargs,
|
158 |
+
stream=True)
|
159 |
+
output = ""
|
160 |
+
for chunk in stream:
|
161 |
+
output += chunk.choices[0].delta.get("content", "")
|
162 |
+
yield output
|
163 |
+
|
164 |
+
except Exception as e:
|
165 |
+
if "Too Many Requests" in str(e):
|
166 |
+
print("ERROR: Too many requests on OpenAI client")
|
167 |
+
gr.Warning("Unfortunately OpenAI is unable to process")
|
168 |
+
return "Unfortunately, I am not able to process your request now."
|
169 |
+
elif "You didn't provide an API key" in str(e):
|
170 |
+
print("Authetification error:", str(e))
|
171 |
+
gr.Warning("Authentication error: OpenAI key was either not provided or incorrect")
|
172 |
+
return "Authentication error"
|
173 |
+
else:
|
174 |
+
print("Unhandled Exception:", str(e))
|
175 |
+
gr.Warning("Unfortunately OpenAI is unable to process")
|
176 |
+
return "I do not know what happened, but I couldn't understand you."
|