Spaces:
Sleeping
Sleeping
File size: 4,353 Bytes
3b44354 67230f1 3b44354 39450a5 3b44354 ae3b3e8 3b44354 ae3b3e8 3b44354 0c57f14 3b44354 ab5be59 52954bf cb3a793 3b44354 9909257 3b44354 ae3b3e8 cec168e ae3b3e8 3b44354 67230f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 |
from openai import OpenAI
import gradio as gr
import requests
from datetime import date
from test_web_rag import get_docs_from_web
import json
import os
from dotenv import load_dotenv
load_dotenv()
# Replace with your key
client = OpenAI()
username = os.getenv("USERNAME")
password = os.getenv('PASSWORD')
def predict(message, history, _web_search_enabled, _n_web_search, _strategy):
# docs = get_web_search_you(message)
partial_message = ''
information = ''
if _web_search_enabled == 'On':
docs = get_docs_from_web(message, history[-1:], _n_web_search, _strategy)
for doc in docs:
if isinstance(doc, dict):
information = doc.get('data')
else:
partial_message = partial_message + doc
yield partial_message
system_prompt = """
You are an advanced chatbot.
Today's date - {date}
When answering a question, adhere to the following revised rules:
- Use the "Information for reference" data to answer the question. If no answer found in "Information for reference" data then try to give answer.
- The "Information for reference" data is provided in the chunks with each chunk having its own source as url.
- Generate human-like text in response to input, reflecting your status as a sophisticated language model.
- Abstain from offering any health or medical advice and ensure all responses maintain this guideline strictly.
- Format all responses in markdown format consistently throughout interactions.
- Must cite sources from the information at the conclusion of your response using properly titled references, but only if the information you provided comes from sources that can be cited.
Information for reference:
"{context}"
Your answer should be structured in markdown as follows:
<Answer>
**Sources**:
Include this section only if the provided information contains sources. If sources are included, list them as follows:
- [Title of Source 1](URL to Source 1)
- [Title of Source 2](URL to Source 2)
... as needed. If no sources are provided, do not include this section in answer.
""".format(context=information, question=message, date=date.today().strftime('%B %d, %Y'))
history_openai_format = [{"role": "system", "content": system_prompt}]
for human, assistant in history:
history_openai_format.append({"role": "user", "content": human})
history_openai_format.append({"role": "assistant", "content": assistant})
history_openai_format.append({"role": "user", "content": message})
# print(history_openai_format)
response = client.chat.completions.create(model='gpt-4-turbo',
messages=history_openai_format,
temperature=0.5,
max_tokens=1000,
top_p=0.5,
stream=True)
partial_message = ''
for chunk in response:
if chunk.choices[0].delta.content is not None:
partial_message = partial_message + chunk.choices[0].delta.content
yield partial_message
print(f"Question:- {message}")
print(f"Answer:- {' '.join(partial_message.split())}")
print('===========================================================================')
n_web_search = gr.Slider(1, 10, value=3, step=1, label="Web searches",
info="Choose between 1 and 10 number of web searches to do. Remember more the web searches more it will take time to reply.")
strategy = gr.Radio(["Deep", "Normal", "Normal Fast"], label="Strategy", value="Normal",
info="Select web search analysis type. Please keep in mind that deep analysis will take more time than normal analysis.")
web_search_enabled = gr.Radio(["On", "Off"], label="Web Search", value="Off",
info="Select web search option on and off.")
app = gr.ChatInterface(predict, additional_inputs=[web_search_enabled, n_web_search, strategy])
app.queue(default_concurrency_limit=5)
app.launch(debug=True, share=False, auth=(username, password), auth_message='Twimbit project ABM')
|