log into logfire
Browse files- app.py +37 -28
- requirements.txt +0 -0
app.py
CHANGED
@@ -5,6 +5,7 @@ import os
|
|
5 |
# Third-party Imports
|
6 |
from dotenv import load_dotenv
|
7 |
import chromadb
|
|
|
8 |
import gradio as gr
|
9 |
from huggingface_hub import snapshot_download
|
10 |
|
@@ -24,6 +25,8 @@ from llama_index.embeddings.adapter import AdapterEmbeddingModel
|
|
24 |
|
25 |
load_dotenv()
|
26 |
|
|
|
|
|
27 |
logger = logging.getLogger(__name__)
|
28 |
logging.basicConfig(level=logging.INFO)
|
29 |
logging.getLogger("httpx").setLevel(logging.WARNING)
|
@@ -153,43 +156,49 @@ def generate_completion(query, history, memory, openAI_api_key, cohere_api_key):
|
|
153 |
return
|
154 |
|
155 |
llm = OpenAI(temperature=1, model="gpt-4o-mini", api_key=openAI_api_key)
|
|
|
|
|
|
|
156 |
|
157 |
# Validate Cohere API Key
|
158 |
if cohere_api_key is None or not cohere_api_key.strip():
|
159 |
logging.error("Cohere API Key is not set or is invalid. Please provide a valid key.")
|
160 |
yield "Error: Cohere API Key is not set or is invalid. Please provide a valid key."
|
161 |
return
|
162 |
-
|
163 |
-
# Manage memory
|
164 |
-
chat_list = memory.get()
|
165 |
-
if len(chat_list) != 0:
|
166 |
-
user_index = [i for i, msg in enumerate(chat_list) if msg.role == MessageRole.USER]
|
167 |
-
if len(user_index) > len(history):
|
168 |
-
user_index_to_remove = user_index[len(history)]
|
169 |
-
chat_list = chat_list[:user_index_to_remove]
|
170 |
-
memory.set(chat_list)
|
171 |
-
logging.info(f"chat_history: {len(memory.get())} {memory.get()}")
|
172 |
-
logging.info(f"gradio_history: {len(history)} {history}")
|
173 |
-
|
174 |
-
# Create agent
|
175 |
-
tools = get_tools(db_collection="azure-architect", cohere_api_key = cohere_api_key )
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
memory
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
|
191 |
-
|
192 |
-
|
193 |
|
194 |
def launch_ui():
|
195 |
|
|
|
5 |
# Third-party Imports
|
6 |
from dotenv import load_dotenv
|
7 |
import chromadb
|
8 |
+
import logfire
|
9 |
import gradio as gr
|
10 |
from huggingface_hub import snapshot_download
|
11 |
|
|
|
25 |
|
26 |
load_dotenv()
|
27 |
|
28 |
+
logfire.configure()
|
29 |
+
|
30 |
logger = logging.getLogger(__name__)
|
31 |
logging.basicConfig(level=logging.INFO)
|
32 |
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
156 |
return
|
157 |
|
158 |
llm = OpenAI(temperature=1, model="gpt-4o-mini", api_key=openAI_api_key)
|
159 |
+
client = llm._get_client()
|
160 |
+
logfire.instrument_openai(client)
|
161 |
+
|
162 |
|
163 |
# Validate Cohere API Key
|
164 |
if cohere_api_key is None or not cohere_api_key.strip():
|
165 |
logging.error("Cohere API Key is not set or is invalid. Please provide a valid key.")
|
166 |
yield "Error: Cohere API Key is not set or is invalid. Please provide a valid key."
|
167 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
+
with logfire.span(f"Running query: {query}"):
|
170 |
+
|
171 |
+
# Manage memory
|
172 |
+
chat_list = memory.get()
|
173 |
+
if len(chat_list) != 0:
|
174 |
+
user_index = [i for i, msg in enumerate(chat_list) if msg.role == MessageRole.USER]
|
175 |
+
if len(user_index) > len(history):
|
176 |
+
user_index_to_remove = user_index[len(history)]
|
177 |
+
chat_list = chat_list[:user_index_to_remove]
|
178 |
+
memory.set(chat_list)
|
179 |
+
|
180 |
+
logfire.info(f"chat_history: {len(memory.get())} {memory.get()}")
|
181 |
+
logfire.info(f"gradio_history: {len(history)} {history}")
|
182 |
+
|
183 |
+
# Create agent
|
184 |
+
tools = get_tools(db_collection="azure-architect", cohere_api_key = cohere_api_key )
|
185 |
+
|
186 |
+
agent = OpenAIAgent.from_tools(
|
187 |
+
llm=llm,
|
188 |
+
memory=memory,
|
189 |
+
tools=tools,
|
190 |
+
system_prompt=PROMPT_SYSTEM_MESSAGE
|
191 |
+
)
|
192 |
|
193 |
+
# Generate answer
|
194 |
+
completion = agent.stream_chat(query)
|
195 |
+
answer_str = ""
|
196 |
+
for token in completion.response_gen:
|
197 |
+
answer_str += token
|
198 |
+
yield answer_str
|
199 |
|
200 |
+
logging.info(f"Source count: {len(completion.sources)}")
|
201 |
+
logging.info(f"Sources: {completion.sources}")
|
202 |
|
203 |
def launch_ui():
|
204 |
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|