GattoNero commited on
Commit
f227f5c
·
verified ·
1 Parent(s): 06fc836

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -30
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
- # Imposta l'LLM (puoi usare anche altri modelli via HuggingFace o OpenRouter)
24
- self.llm = OpenAI(
 
 
 
 
 
25
  model="gpt-4o-mini",
26
  temperature=0,
27
- api_key=os.getenv("OPENAI_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 directory "data/"
38
  self.documents = SimpleDirectoryReader("data").load_data()
39
-
40
- # Crea un indice vettoriale
41
- self.index = VectorStoreIndex.from_documents(
42
- self.documents, service_context=self.service_context
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"Agent received question (first 50 chars): {question[:50]}...")
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):