GattoNero commited on
Commit
4a510d9
·
verified ·
1 Parent(s): 09826d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -37
app.py CHANGED
@@ -37,45 +37,51 @@ logging.getLogger().setLevel(logging.DEBUG) # imposta il livello di log a DEBUG
37
 
38
  class BasicAgent:
39
  def __init__(self):
40
- print("coso Initializing LlamaIndex-based agent...")
41
-
42
- # Leggi la chiave OpenAI dall'ambiente
43
- openai_api_key = os.getenv("OPENAI_API_KEY")
44
- if not openai_api_key:
45
- raise ValueError("OPENAI_API_KEY not set!")
46
-
47
- # Imposta il logger
48
- logging.basicConfig(level=logging.DEBUG)
49
-
50
- # LLM per LlamaIndex
51
- llm = OpenAI(
52
- api_key=openai_api_key
53
- )
54
- Settings.llm = llm
55
-
56
- # Tool per estrarre ingredienti
57
- ingredient_tool = FunctionTool.from_defaults(
58
- name="extract_ingredients",
59
- fn=extract_ingredients,
60
- description="Extracts and returns a comma-separated, alphabetized list of ingredients for a pie filling from a transcription string."
61
- )
62
-
63
- # Registra il tool
64
- Settings.tools = [ingredient_tool]
65
-
66
- # Prepara l'agente
67
- self.agent = OpenAIAgent.from_tools([ingredient_tool], llm=llm, verbose=True)
68
-
69
- # Client OpenAI per chiamate esterne (immagini/audio)
70
- self.client = OpenAI(api_key=openai_api_key)
71
-
72
- # Carica i documenti
73
- self.documents = SimpleDirectoryReader("data").load_data()
74
- self.index = VectorStoreIndex.from_documents(self.documents, settings=Settings)
75
- self.query_engine = self.index.as_query_engine()
 
 
 
76
 
77
- print("coso Agent ready.")
78
 
 
 
 
 
79
 
80
 
81
 
 
37
 
38
  class BasicAgent:
39
  def __init__(self):
40
+ try:
41
+ print("coso Initializing LlamaIndex-based agent...")
42
+
43
+ # Leggi la chiave OpenAI dall'ambiente
44
+ openai_api_key = os.getenv("OPENAI_API_KEY")
45
+ if not openai_api_key:
46
+ raise ValueError("OPENAI_API_KEY not set!")
47
+
48
+ # Imposta il logger
49
+ logging.basicConfig(level=logging.DEBUG)
50
+
51
+ # LLM per LlamaIndex
52
+ llm = OpenAI(
53
+ api_key=openai_api_key
54
+ )
55
+ Settings.llm = llm
56
+
57
+ # Tool per estrarre ingredienti
58
+ ingredient_tool = FunctionTool.from_defaults(
59
+ name="extract_ingredients",
60
+ fn=extract_ingredients,
61
+ description="Extracts and returns a comma-separated, alphabetized list of ingredients for a pie filling from a transcription string."
62
+ )
63
+
64
+ # Registra il tool
65
+ Settings.tools = [ingredient_tool]
66
+
67
+ # Prepara l'agente
68
+ self.agent = OpenAIAgent.from_tools([ingredient_tool], llm=llm, verbose=True)
69
+
70
+ # Client OpenAI per chiamate esterne (immagini/audio)
71
+ self.client = OpenAI(api_key=openai_api_key)
72
+
73
+ # Carica i documenti
74
+ self.documents = SimpleDirectoryReader("data").load_data()
75
+ self.index = VectorStoreIndex.from_documents(self.documents, settings=Settings)
76
+ self.query_engine = self.index.as_query_engine()
77
+
78
+ print("coso Agent ready.")
79
 
 
80
 
81
+ except Exception as e:
82
+ import traceback
83
+ print_coso("Error instantiating agent:", e)
84
+ traceback.print_exc()
85
 
86
 
87