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