Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,56 +7,46 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, OpenAIServer
|
|
7 |
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
8 |
from llama_index.llms import OpenAI
|
9 |
from llama_index.settings import Settings
|
|
|
10 |
|
11 |
-
|
12 |
-
# (Keep Constants as is)
|
13 |
-
# --- Constants ---
|
14 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
15 |
-
|
16 |
-
# --- Basic Agent Definition ---
|
17 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
18 |
class BasicAgent:
|
19 |
-
|
20 |
def __init__(self):
|
21 |
print("Initializing LlamaIndex-based agent...")
|
22 |
-
|
23 |
-
#
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
model="gpt-4o-mini",
|
26 |
temperature=0,
|
27 |
-
api_key=
|
28 |
)
|
29 |
-
|
30 |
-
#OpenAI(model="gpt-3.5-turbo", temperature=0)
|
31 |
-
|
32 |
# Imposta le impostazioni tramite Settings
|
33 |
settings = Settings(
|
34 |
llm=llm
|
35 |
)
|
36 |
-
|
37 |
-
# Carica i documenti dalla
|
38 |
self.documents = SimpleDirectoryReader("data").load_data()
|
39 |
-
|
40 |
-
# Crea
|
41 |
-
self.index = VectorStoreIndex.from_documents(
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
# Crea il query engine
|
46 |
self.query_engine = self.index.as_query_engine()
|
|
|
47 |
|
48 |
def __call__(self, question: str) -> str:
|
49 |
-
print(f"
|
50 |
response = self.query_engine.query(question)
|
51 |
-
print(f"Agent returning response: {response}")
|
52 |
return str(response)
|
53 |
|
54 |
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
##Roba per la valutazione
|
61 |
|
62 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
7 |
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
8 |
from llama_index.llms import OpenAI
|
9 |
from llama_index.settings import Settings
|
10 |
+
import os
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
class BasicAgent:
|
|
|
13 |
def __init__(self):
|
14 |
print("Initializing LlamaIndex-based agent...")
|
15 |
+
|
16 |
+
# Leggi la chiave OpenAI dall'ambiente
|
17 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
18 |
+
if not openai_api_key:
|
19 |
+
raise ValueError("OPENAI_API_KEY not set!")
|
20 |
+
|
21 |
+
# Crea un'istanza di OpenAI
|
22 |
+
llm = OpenAI(
|
23 |
model="gpt-4o-mini",
|
24 |
temperature=0,
|
25 |
+
api_key=openai_api_key
|
26 |
)
|
27 |
+
|
|
|
|
|
28 |
# Imposta le impostazioni tramite Settings
|
29 |
settings = Settings(
|
30 |
llm=llm
|
31 |
)
|
32 |
+
|
33 |
+
# Carica i documenti dalla cartella 'data'
|
34 |
self.documents = SimpleDirectoryReader("data").load_data()
|
35 |
+
|
36 |
+
# Crea l'indice con la configurazione
|
37 |
+
self.index = VectorStoreIndex.from_documents(self.documents, settings=settings)
|
38 |
+
|
39 |
+
# Prepara il query engine
|
|
|
|
|
40 |
self.query_engine = self.index.as_query_engine()
|
41 |
+
print("Agent ready.")
|
42 |
|
43 |
def __call__(self, question: str) -> str:
|
44 |
+
print(f"Received question: {question[:50]}...")
|
45 |
response = self.query_engine.query(question)
|
|
|
46 |
return str(response)
|
47 |
|
48 |
|
49 |
|
|
|
|
|
|
|
|
|
50 |
##Roba per la valutazione
|
51 |
|
52 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|