Du Mingzhe
commited on
Commit
•
88f52d0
1
Parent(s):
e6160ba
Update
Browse files- app.py +3 -2
- components.py +13 -4
- playground.ipynb +0 -42
app.py
CHANGED
@@ -8,7 +8,7 @@ st.title("Talk with Mingzhe")
|
|
8 |
|
9 |
llm_client = LLMClient(api_key=st.secrets["OPENAI_API_KEY"], model_name="gpt-4-turbo-preview")
|
10 |
pinecone_client = PersonalIndexClient(index_token=st.secrets["PINECONE_API_KEY"], embedding_token=st.secrets["OPENAI_API_KEY"], embedding_model_name='text-embedding-3-large', index_name='mingzhe')
|
11 |
-
web_searcher = WebSearcher(
|
12 |
|
13 |
if "messages" not in st.session_state:
|
14 |
st.session_state.messages = []
|
@@ -32,7 +32,8 @@ if prompt := st.chat_input("What's up?"):
|
|
32 |
web_query = llm_client.web_query_generator(query=prompt, history=st.session_state.messages)
|
33 |
print(f"Web Query: {web_query}")
|
34 |
|
35 |
-
web_result = web_searcher.
|
|
|
36 |
print(f"Web Result: {web_result}")
|
37 |
|
38 |
stream = llm_client.response_generate(prompt, st.session_state.messages, memory, web_result)
|
|
|
8 |
|
9 |
llm_client = LLMClient(api_key=st.secrets["OPENAI_API_KEY"], model_name="gpt-4-turbo-preview")
|
10 |
pinecone_client = PersonalIndexClient(index_token=st.secrets["PINECONE_API_KEY"], embedding_token=st.secrets["OPENAI_API_KEY"], embedding_model_name='text-embedding-3-large', index_name='mingzhe')
|
11 |
+
web_searcher = WebSearcher(you_api_key=st.secrets["YOU_API_KEY"], bing_api_key=st.secrets["BING_API_KEY"])
|
12 |
|
13 |
if "messages" not in st.session_state:
|
14 |
st.session_state.messages = []
|
|
|
32 |
web_query = llm_client.web_query_generator(query=prompt, history=st.session_state.messages)
|
33 |
print(f"Web Query: {web_query}")
|
34 |
|
35 |
+
web_result = web_searcher.query_bing(query=prompt) if web_query != 'None' else "None"
|
36 |
+
# web_result = web_searcher.query_web_llm(query=prompt, num_web_results=10) if web_query != 'None' else "None"
|
37 |
print(f"Web Result: {web_result}")
|
38 |
|
39 |
stream = llm_client.response_generate(prompt, st.session_state.messages, memory, web_result)
|
components.py
CHANGED
@@ -133,12 +133,21 @@ class PersonalIndexClient(object):
|
|
133 |
return pinecone_memory
|
134 |
|
135 |
class WebSearcher(object):
|
136 |
-
def __init__(self,
|
137 |
-
self.
|
|
|
138 |
pass
|
139 |
|
140 |
def query_web_llm(self, query, num_web_results=5):
|
141 |
-
headers = {"X-API-Key": self.
|
142 |
params = {"query": query, 'num_web_results': num_web_results}
|
143 |
response_json = requests.get(f"https://api.ydc-index.io/rag?query={query}", params=params, headers=headers).json()
|
144 |
-
return response_json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
return pinecone_memory
|
134 |
|
135 |
class WebSearcher(object):
|
136 |
+
def __init__(self, you_api_key, bing_api_key) -> None:
|
137 |
+
self.you_api_key = you_api_key
|
138 |
+
self.bing_api_key = bing_api_key
|
139 |
pass
|
140 |
|
141 |
def query_web_llm(self, query, num_web_results=5):
|
142 |
+
headers = {"X-API-Key": self.you_api_key}
|
143 |
params = {"query": query, 'num_web_results': num_web_results}
|
144 |
response_json = requests.get(f"https://api.ydc-index.io/rag?query={query}", params=params, headers=headers).json()
|
145 |
+
return response_json
|
146 |
+
|
147 |
+
def query_bing(self, query):
|
148 |
+
headers = {"Ocp-Apim-Subscription-Key": self.bing_api_key}
|
149 |
+
params = {"q": query, "textDecorations": True, "textFormat": "HTML"}
|
150 |
+
response = requests.get("https://api.bing.microsoft.com/v7.0/search", headers=headers, params=params)
|
151 |
+
response.raise_for_status()
|
152 |
+
search_results = response.json()
|
153 |
+
return search_results
|
playground.ipynb
CHANGED
@@ -9,48 +9,6 @@
|
|
9 |
"import uuid"
|
10 |
]
|
11 |
},
|
12 |
-
{
|
13 |
-
"cell_type": "code",
|
14 |
-
"execution_count": 3,
|
15 |
-
"metadata": {},
|
16 |
-
"outputs": [],
|
17 |
-
"source": [
|
18 |
-
"sid = uuid.uuid1()"
|
19 |
-
]
|
20 |
-
},
|
21 |
-
{
|
22 |
-
"cell_type": "code",
|
23 |
-
"execution_count": 5,
|
24 |
-
"metadata": {},
|
25 |
-
"outputs": [
|
26 |
-
{
|
27 |
-
"data": {
|
28 |
-
"text/plain": [
|
29 |
-
"'4d742a38dea811ee8aad1e30bc5366a5'"
|
30 |
-
]
|
31 |
-
},
|
32 |
-
"execution_count": 5,
|
33 |
-
"metadata": {},
|
34 |
-
"output_type": "execute_result"
|
35 |
-
}
|
36 |
-
],
|
37 |
-
"source": [
|
38 |
-
"sid.hex"
|
39 |
-
]
|
40 |
-
},
|
41 |
-
{
|
42 |
-
"cell_type": "code",
|
43 |
-
"execution_count": null,
|
44 |
-
"metadata": {},
|
45 |
-
"outputs": [],
|
46 |
-
"source": [
|
47 |
-
"pi.create(data=[{\n",
|
48 |
-
" 'id': '123',\n",
|
49 |
-
" 'content': '123',\n",
|
50 |
-
" 'metadata': {'hello': 'world'},\n",
|
51 |
-
" }])"
|
52 |
-
]
|
53 |
-
},
|
54 |
{
|
55 |
"cell_type": "code",
|
56 |
"execution_count": null,
|
|
|
9 |
"import uuid"
|
10 |
]
|
11 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
{
|
13 |
"cell_type": "code",
|
14 |
"execution_count": null,
|