twimbit-ai commited on
Commit
3b44354
·
verified ·
1 Parent(s): fb6c430

Create chat_bot.py

Browse files
Files changed (1) hide show
  1. chat_bot.py +109 -0
chat_bot.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import OpenAI
2
+ import gradio as gr
3
+ import requests
4
+ from datetime import date
5
+ from test_web_rag import get_docs_from_web
6
+ import json
7
+ import os
8
+ from dotenv import load_dotenv
9
+
10
+ load_dotenv()
11
+ # Replace with your key
12
+ client = OpenAI()
13
+ you_key = os.getenv("YOU_API_KEY")
14
+
15
+
16
+ def get_ai_snippets_for_query(query):
17
+ headers = {"X-API-Key": you_key}
18
+ params = {"query": query}
19
+ return requests.get(
20
+ f"https://api.ydc-index.io/search?query={query}",
21
+ params=params,
22
+ headers=headers,
23
+ ).json().get('hits')
24
+
25
+
26
+ def get_web_search_you(query):
27
+ docs = get_ai_snippets_for_query(query)
28
+ markdown = ""
29
+ for doc in docs:
30
+ for key, value in doc.items():
31
+ if key == 'snippets':
32
+ markdown += f"{key}:\n"
33
+ for snippet in value:
34
+ markdown += f"- {snippet}\n"
35
+ else:
36
+ markdown += f"{key}: {value}\n"
37
+ markdown += "\n"
38
+ return markdown
39
+
40
+
41
+ def predict(message, history, _n_web_search, _strategy):
42
+ # docs = get_web_search_you(message)
43
+
44
+ with open('history.json', mode='a', encoding='utf-8') as f:
45
+ json.dump(history, f)
46
+ docs = get_docs_from_web(message, history[-1:], _n_web_search, _strategy)
47
+ partial_message = ''
48
+ information = ''
49
+ for doc in docs:
50
+ if isinstance(doc, dict):
51
+ information = doc.get('data')
52
+ else:
53
+ partial_message = partial_message + doc
54
+ yield partial_message
55
+ system_prompt = """
56
+ You are an advanced chatbot.
57
+ Today's date - {date}
58
+
59
+ When answering a question, adhere to the following revised rules:
60
+ - The "Information for reference" data is provided in the chunks with each chunk having its own source as url.
61
+ - Generate human-like text in response to input, reflecting your status as a sophisticated language model.
62
+ - Abstain from offering any health or medical advice and ensure all responses maintain this guideline strictly.
63
+ - Format all responses in markdown format consistently throughout interactions.
64
+ - 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.
65
+
66
+ Information for reference:
67
+ "{context}"
68
+
69
+ Your answer should be structured in markdown as follows:
70
+
71
+
72
+ <Answer>
73
+
74
+
75
+ **Sources**:
76
+ Include this section only if the provided information contains sources. If sources are included, list them as follows:
77
+ - [Title of Source 1](URL to Source 1)
78
+ - [Title of Source 2](URL to Source 2)
79
+ ... as needed. If no sources are provided, do not include this section in answer.
80
+ """.format(context=information, question=message, date=date.today().strftime('%B %d, %Y'))
81
+
82
+ history_openai_format = [{"role": "system", "content": system_prompt}]
83
+ for human, assistant in history:
84
+ history_openai_format.append({"role": "user", "content": human})
85
+ history_openai_format.append({"role": "assistant", "content": assistant})
86
+ history_openai_format.append({"role": "user", "content": message})
87
+ # print(history_openai_format)
88
+
89
+ response = client.chat.completions.create(model='gpt-4-turbo',
90
+ messages=history_openai_format,
91
+ temperature=0.5,
92
+ max_tokens=1000,
93
+ top_p=0.5,
94
+ stream=True)
95
+ partial_message += '\n\n'
96
+ for chunk in response:
97
+ if chunk.choices[0].delta.content is not None:
98
+ partial_message = partial_message + chunk.choices[0].delta.content
99
+ yield partial_message
100
+
101
+
102
+ n_web_search = gr.Slider(1, 10, value=3, step=1, label="Web searches",
103
+ info="Choose between 1 and 10 number of web searches to do. Remember more the web searches more it will take time to reply.")
104
+ strategy = gr.Radio(["Deep", "Normal"], label="Strategy", value="Normal",
105
+ info="Select web search analysis type. Please keep in mind that deep analysis will take more time than normal analysis.")
106
+
107
+ app = gr.ChatInterface(predict, additional_inputs=[n_web_search, strategy])
108
+ app.queue(default_concurrency_limit=5)
109
+ app.launch(debug=True, share=False)