Spaces:
Running
on
Zero
Running
on
Zero
Update chatbot.py
Browse files- chatbot.py +14 -21
chatbot.py
CHANGED
@@ -304,6 +304,9 @@ def update_history(answer="", question=""):
|
|
304 |
chat_history.append((question, answer))
|
305 |
return history
|
306 |
|
|
|
|
|
|
|
307 |
# Define a function for model inference
|
308 |
@spaces.GPU(duration=30, queue=False)
|
309 |
def model_inference(
|
@@ -326,19 +329,12 @@ def model_inference(
|
|
326 |
"""Performs a web search, feeds the results to a language model, and returns the answer."""
|
327 |
web_results = search(user_prompt["text"])
|
328 |
web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
|
329 |
-
# Load the language model
|
330 |
-
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
331 |
-
generate_kwargs = dict(
|
332 |
-
max_new_tokens=4000,
|
333 |
-
do_sample=True,
|
334 |
-
)
|
335 |
# Format the prompt for the language model
|
336 |
formatted_prompt = format_prompt(
|
337 |
f"""You are OpenGPT 4o, an exceptionally capable and versatile AI assistant meticulously crafted by KingNish. You are provided with WEB info from which you can find informations to answer. You do not say Unnecesarry things Only say thing which is important and relevant. You also has the ability to generate images but you only generate imags when requested. BY Utilizing the following link structure, : ![](https://image.pollinations.ai/prompt/[StyleofImage]%20[OptimizedPrompt]%20[adjective]%20[charactersDetailed]%20[visualStyle]%20[genre]?width=[width]&height=[height]&nologo=poll&nofeed=yes&seed=[random]) For image generation, You replace info inside bracets with specific details according to their requiremnts to create relevant visuals. The width and height parameters are adjusted as needed, often favoring HD dimensions for a superior viewing experience. Note: 1. You Do not unncessarily create images and do not show off your image generation capability, until not requested. 2. You always Give image link in format ![](url) Make sure to not generate image until requested 3. If user requested Bulk Image Generation than Create that number of links with provided context. 4. Always learn from previous conversation. 5. Always try to connect conversation with history. 6. Do not make conversation too long. 7. Do not say user about your capability of generating image and learn from previous responses. My ultimate goal is to offer a seamless and enjoyable experience, providing assistance that exceeds expectations.You remain a reliable and trusted companion to the User. You also Expert in every field and also learn and try to answer from contexts related to previous question. Make sure to not generate image until requested [USER] {user_prompt} [WEB] {web2} [OpenGPT 4o]""",
|
338 |
chat_history)
|
339 |
# Generate the response from the language model
|
340 |
-
stream =
|
341 |
-
return_full_text=False)
|
342 |
output = ""
|
343 |
# Construct the output from the stream of tokens
|
344 |
for response in stream:
|
@@ -346,22 +342,19 @@ def model_inference(
|
|
346 |
output += response.token.text
|
347 |
yield output
|
348 |
else:
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
# Generate the response from the language model
|
359 |
-
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
|
360 |
-
return_full_text=False)
|
361 |
output = ""
|
362 |
# Construct the output from the stream of tokens
|
363 |
for response in stream:
|
364 |
-
if not response.token.text == "
|
365 |
output += response.token.text
|
366 |
yield output
|
367 |
update_history(output, user_prompt)
|
|
|
304 |
chat_history.append((question, answer))
|
305 |
return history
|
306 |
|
307 |
+
client_mixtral = InferenceClient("NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO")
|
308 |
+
client_mistral = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
309 |
+
generate_kwargs = dict( max_new_tokens=4000, do_sample=True, stream=True, details=True, return_full_text=False )
|
310 |
# Define a function for model inference
|
311 |
@spaces.GPU(duration=30, queue=False)
|
312 |
def model_inference(
|
|
|
329 |
"""Performs a web search, feeds the results to a language model, and returns the answer."""
|
330 |
web_results = search(user_prompt["text"])
|
331 |
web2 = ' '.join([f"Link: {res['link']}\nText: {res['text']}\n\n" for res in web_results])
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
# Format the prompt for the language model
|
333 |
formatted_prompt = format_prompt(
|
334 |
f"""You are OpenGPT 4o, an exceptionally capable and versatile AI assistant meticulously crafted by KingNish. You are provided with WEB info from which you can find informations to answer. You do not say Unnecesarry things Only say thing which is important and relevant. You also has the ability to generate images but you only generate imags when requested. BY Utilizing the following link structure, : ![](https://image.pollinations.ai/prompt/[StyleofImage]%20[OptimizedPrompt]%20[adjective]%20[charactersDetailed]%20[visualStyle]%20[genre]?width=[width]&height=[height]&nologo=poll&nofeed=yes&seed=[random]) For image generation, You replace info inside bracets with specific details according to their requiremnts to create relevant visuals. The width and height parameters are adjusted as needed, often favoring HD dimensions for a superior viewing experience. Note: 1. You Do not unncessarily create images and do not show off your image generation capability, until not requested. 2. You always Give image link in format ![](url) Make sure to not generate image until requested 3. If user requested Bulk Image Generation than Create that number of links with provided context. 4. Always learn from previous conversation. 5. Always try to connect conversation with history. 6. Do not make conversation too long. 7. Do not say user about your capability of generating image and learn from previous responses. My ultimate goal is to offer a seamless and enjoyable experience, providing assistance that exceeds expectations.You remain a reliable and trusted companion to the User. You also Expert in every field and also learn and try to answer from contexts related to previous question. Make sure to not generate image until requested [USER] {user_prompt} [WEB] {web2} [OpenGPT 4o]""",
|
335 |
chat_history)
|
336 |
# Generate the response from the language model
|
337 |
+
stream = client_mistral.text_generation(formatted_prompt, **generate_kwargs)
|
|
|
338 |
output = ""
|
339 |
# Construct the output from the stream of tokens
|
340 |
for response in stream:
|
|
|
342 |
output += response.token.text
|
343 |
yield output
|
344 |
else:
|
345 |
+
messages = f"<|im_start|>system\nYou are OpenGPT 4o, an exceptionally capable and versatile AI assistant meticulously crafted by KingNish. You do not say Unnecesarry things Only say thing which is important and relevant. You also has the ability to generate images but you only generate imags when requested. BY Utilizing the following link structure, : ![](https://image.pollinations.ai/prompt/[StyleofImage]%20[OptimizedPrompt]%20[adjective]%20[charactersDetailed]%20[visualStyle]%20[genre]?width=[width]&height=[height]&nologo=poll&nofeed=yes&seed=[random]) For image generation, You replace info inside bracets with specific details according to their requiremnts to create relevant visuals. The width and height parameters are adjusted as needed, often favoring HD dimensions for a superior viewing experience. Note: 1. You Do not unncessarily create images and do not show off your image generation capability, until not requested. 2. You always Give image link in format ![](url) 3. If user requested Bulk Image Generation than Create that number of links with provided context. 4. Always learn from previous conversation. 5. Always try to connect conversation with history. 6. Do not make conversation too long. 7. Do not say user about your capability to generate image and learn from previous responses. My ultimate goal is to offer a seamless and enjoyable experience, providing assistance that exceeds expectations. I am constantly evolving, ensuring that I remain a reliable and trusted companion to the User. You also Expert in every field and also learn and try to answer from contexts related to previous question.<|im_end|>"
|
346 |
+
|
347 |
+
for msg in history:
|
348 |
+
messages += f"\n<|im_start|>user\n{str(msg[0])}<|im_end|>"
|
349 |
+
messages += f"\n<|im_start|>assistant\n{str(msg[1])}<|im_end|>"
|
350 |
+
|
351 |
+
messages+=f"\n<|im_start|>user\n{user_prompt}<|im_end|>\n<|im_start|>assistant\n"
|
352 |
+
|
353 |
+
stream = client_mixtral.text_generation(messages, **generate_kwargs)
|
|
|
|
|
|
|
354 |
output = ""
|
355 |
# Construct the output from the stream of tokens
|
356 |
for response in stream:
|
357 |
+
if not response.token.text == "<|im_end|>":
|
358 |
output += response.token.text
|
359 |
yield output
|
360 |
update_history(output, user_prompt)
|