Spaces:
Sleeping
Sleeping
Omar Solano
commited on
Commit
β’
b952f44
1
Parent(s):
f4f4bac
remove commented code
Browse files- scripts/gradio-ui.py +14 -102
scripts/gradio-ui.py
CHANGED
@@ -1,8 +1,5 @@
|
|
1 |
-
import logging
|
2 |
import os
|
3 |
import pickle
|
4 |
-
from datetime import datetime
|
5 |
-
from typing import Optional
|
6 |
|
7 |
import chromadb
|
8 |
import gradio as gr
|
@@ -10,64 +7,23 @@ import logfire
|
|
10 |
from custom_retriever import CustomRetriever
|
11 |
from dotenv import load_dotenv
|
12 |
from llama_index.agent.openai import OpenAIAgent
|
13 |
-
from llama_index.core import VectorStoreIndex
|
14 |
-
from llama_index.core.agent import AgentRunner, ReActAgent
|
15 |
-
|
16 |
-
# from llama_index.core.chat_engine import (
|
17 |
-
# CondensePlusContextChatEngine,
|
18 |
-
# CondenseQuestionChatEngine,
|
19 |
-
# ContextChatEngine,
|
20 |
-
# )
|
21 |
-
from llama_index.core.data_structs import Node
|
22 |
from llama_index.core.llms import MessageRole
|
23 |
from llama_index.core.memory import ChatMemoryBuffer
|
24 |
from llama_index.core.node_parser import SentenceSplitter
|
25 |
-
from llama_index.core.query_engine import RetrieverQueryEngine
|
26 |
from llama_index.core.retrievers import VectorIndexRetriever
|
27 |
-
from llama_index.core.tools import
|
28 |
-
FunctionTool,
|
29 |
-
QueryEngineTool,
|
30 |
-
RetrieverTool,
|
31 |
-
ToolMetadata,
|
32 |
-
)
|
33 |
-
|
34 |
-
# from llama_index.core.vector_stores import (
|
35 |
-
# ExactMatchFilter,
|
36 |
-
# FilterCondition,
|
37 |
-
# FilterOperator,
|
38 |
-
# MetadataFilter,
|
39 |
-
# MetadataFilters,
|
40 |
-
# )
|
41 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
42 |
-
from llama_index.llms.gemini import Gemini
|
43 |
from llama_index.llms.openai import OpenAI
|
44 |
-
from llama_index.llms.openai.utils import GPT4_MODELS
|
45 |
from llama_index.vector_stores.chroma import ChromaVectorStore
|
46 |
-
from tutor_prompts import
|
47 |
-
TEXT_QA_TEMPLATE,
|
48 |
-
QueryValidation,
|
49 |
-
system_message_openai_agent,
|
50 |
-
system_message_validation,
|
51 |
-
system_prompt,
|
52 |
-
)
|
53 |
-
|
54 |
-
load_dotenv()
|
55 |
-
|
56 |
|
57 |
# from utils import init_mongo_db
|
58 |
|
59 |
-
|
60 |
-
|
61 |
logfire.configure()
|
62 |
-
# logging.basicConfig(handlers=[logfire.LogfireLoggingHandler("INFO")])
|
63 |
-
# logger = logging.getLogger(__name__)
|
64 |
|
65 |
-
# # This variables are used to intercept API calls
|
66 |
-
# # launch mitmweb
|
67 |
-
# cert_file = "/Users/omar/Documents/mitmproxy-ca-cert.pem"
|
68 |
-
# os.environ["REQUESTS_CA_BUNDLE"] = cert_file
|
69 |
-
# os.environ["SSL_CERT_FILE"] = cert_file
|
70 |
-
# os.environ["HTTPS_PROXY"] = "http://127.0.0.1:8080"
|
71 |
|
72 |
CONCURRENCY_COUNT = int(os.getenv("CONCURRENCY_COUNT", 64))
|
73 |
MONGODB_URI = os.getenv("MONGODB_URI")
|
@@ -131,7 +87,6 @@ index = VectorStoreIndex.from_vector_store(
|
|
131 |
use_async=True,
|
132 |
)
|
133 |
vector_retriever = VectorIndexRetriever(
|
134 |
-
# filters=filters,
|
135 |
index=index,
|
136 |
similarity_top_k=10,
|
137 |
use_async=True,
|
@@ -204,12 +159,10 @@ def generate_completion(
|
|
204 |
chat_list = memory.get()
|
205 |
|
206 |
if len(chat_list) != 0:
|
207 |
-
# Compute number of interactions
|
208 |
user_index = [
|
209 |
i for i, msg in enumerate(chat_list) if msg.role == MessageRole.USER
|
210 |
]
|
211 |
if len(user_index) > len(history):
|
212 |
-
# A message was removed, need to update the memory
|
213 |
user_index_to_remove = user_index[len(history)]
|
214 |
chat_list = chat_list[:user_index_to_remove]
|
215 |
memory.set(chat_list)
|
@@ -237,40 +190,9 @@ def generate_completion(
|
|
237 |
# )
|
238 |
# custom_retriever = CustomRetriever(vector_retriever, document_dict)
|
239 |
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
model=f"models/{model}",
|
244 |
-
temperature=1,
|
245 |
-
max_tokens=None,
|
246 |
-
)
|
247 |
-
else:
|
248 |
-
llm = OpenAI(temperature=1, model=model, max_tokens=None)
|
249 |
-
client = llm._get_client()
|
250 |
-
logfire.instrument_openai(client)
|
251 |
-
|
252 |
-
# response_synthesizer = get_response_synthesizer(
|
253 |
-
# llm=llm,
|
254 |
-
# response_mode="simple_summarize",
|
255 |
-
# text_qa_template=TEXT_QA_TEMPLATE,
|
256 |
-
# streaming=True,
|
257 |
-
# )
|
258 |
-
|
259 |
-
# custom_query_engine = RetrieverQueryEngine(
|
260 |
-
# retriever=custom_retriever,
|
261 |
-
# response_synthesizer=response_synthesizer,
|
262 |
-
# )
|
263 |
-
|
264 |
-
# agent = CondensePlusContextChatEngine.from_defaults(
|
265 |
-
# agent = CondenseQuestionChatEngine.from_defaults(
|
266 |
-
|
267 |
-
# agent = ContextChatEngine.from_defaults(
|
268 |
-
# retriever=custom_retriever,
|
269 |
-
# context_template=system_prompt,
|
270 |
-
# llm=llm,
|
271 |
-
# memory=memory,
|
272 |
-
# verbose=True,
|
273 |
-
# )
|
274 |
|
275 |
query_engine_tools = [
|
276 |
RetrieverTool(
|
@@ -282,23 +204,13 @@ def generate_completion(
|
|
282 |
)
|
283 |
]
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
# system_prompt=system_message_openai_agent,
|
292 |
-
)
|
293 |
-
else:
|
294 |
-
agent = OpenAIAgent.from_tools(
|
295 |
-
llm=llm,
|
296 |
-
memory=memory,
|
297 |
-
tools=query_engine_tools, # type: ignore
|
298 |
-
system_prompt=system_message_openai_agent,
|
299 |
-
)
|
300 |
|
301 |
-
# completion = custom_query_engine.query(query)
|
302 |
completion = agent.stream_chat(query)
|
303 |
|
304 |
answer_str = ""
|
|
|
|
|
1 |
import os
|
2 |
import pickle
|
|
|
|
|
3 |
|
4 |
import chromadb
|
5 |
import gradio as gr
|
|
|
7 |
from custom_retriever import CustomRetriever
|
8 |
from dotenv import load_dotenv
|
9 |
from llama_index.agent.openai import OpenAIAgent
|
10 |
+
from llama_index.core import VectorStoreIndex
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
from llama_index.core.llms import MessageRole
|
12 |
from llama_index.core.memory import ChatMemoryBuffer
|
13 |
from llama_index.core.node_parser import SentenceSplitter
|
|
|
14 |
from llama_index.core.retrievers import VectorIndexRetriever
|
15 |
+
from llama_index.core.tools import RetrieverTool, ToolMetadata
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
|
|
17 |
from llama_index.llms.openai import OpenAI
|
|
|
18 |
from llama_index.vector_stores.chroma import ChromaVectorStore
|
19 |
+
from tutor_prompts import system_message_openai_agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# from utils import init_mongo_db
|
22 |
|
23 |
+
load_dotenv()
|
24 |
+
|
25 |
logfire.configure()
|
|
|
|
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
CONCURRENCY_COUNT = int(os.getenv("CONCURRENCY_COUNT", 64))
|
29 |
MONGODB_URI = os.getenv("MONGODB_URI")
|
|
|
87 |
use_async=True,
|
88 |
)
|
89 |
vector_retriever = VectorIndexRetriever(
|
|
|
90 |
index=index,
|
91 |
similarity_top_k=10,
|
92 |
use_async=True,
|
|
|
159 |
chat_list = memory.get()
|
160 |
|
161 |
if len(chat_list) != 0:
|
|
|
162 |
user_index = [
|
163 |
i for i, msg in enumerate(chat_list) if msg.role == MessageRole.USER
|
164 |
]
|
165 |
if len(user_index) > len(history):
|
|
|
166 |
user_index_to_remove = user_index[len(history)]
|
167 |
chat_list = chat_list[:user_index_to_remove]
|
168 |
memory.set(chat_list)
|
|
|
190 |
# )
|
191 |
# custom_retriever = CustomRetriever(vector_retriever, document_dict)
|
192 |
|
193 |
+
llm = OpenAI(temperature=1, model=model, max_tokens=None)
|
194 |
+
client = llm._get_client()
|
195 |
+
logfire.instrument_openai(client)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
query_engine_tools = [
|
198 |
RetrieverTool(
|
|
|
204 |
)
|
205 |
]
|
206 |
|
207 |
+
agent = OpenAIAgent.from_tools(
|
208 |
+
llm=llm,
|
209 |
+
memory=memory,
|
210 |
+
tools=query_engine_tools, # type: ignore
|
211 |
+
system_prompt=system_message_openai_agent,
|
212 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
|
|
|
214 |
completion = agent.stream_chat(query)
|
215 |
|
216 |
answer_str = ""
|