Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
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 |
|