Spaces:
Sleeping
Sleeping
Update agent.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159import osfrom transformers import pipeline, AutoTokenizer, AutoModelForCausalLMimport torch # För att kontrollera enheter# Importera ditt nya sökverktygfrom tools.tavily_search import search_tavilyclass GaiaAgent:def __init__(self, model_id: str = "google/gemma-2b-it"):# Ladda tokenizer och modell manuellt. Detta ger mer kontroll.try:print(f"Laddar tokenizer för {model_id}...")self.tokenizer = AutoTokenizer.from_pretrained(model_id, token=os.getenv("HF_TOKEN"))print(f"Laddar modell för {model_id}...")# Kontrollera om GPU är tillgängligdevice = "cuda" if torch.cuda.is_available() else "cpu"print(f"Använder enhet: {device}")self.model = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype=torch.bfloat16, # Använd bfloat16 för minskat minnedevice_map="auto", # Accelerate hanterar detta över CPU/GPUtoken=os.getenv("HF_TOKEN"))print("Modell laddad framgångsrikt.")# Skapa en pipeline för textgenereringself.text_generator = pipeline("text-generation",model=self.model,tokenizer=self.tokenizer,# device=0 if device == "cuda" else -1 # 0 för första GPU, -1 för CPU)print("Textgenereringspipeline skapad.")except Exception as e:print(f"Fel vid initiering av agent: {e}")raise RuntimeError(f"Fel vid laddning av modell eller tokenizer: {e}")# --- THIS IS THE MISSING __CALL__ METHOD ---def __call__(self, question: str) -> str:"""Denna metod gör att en instans av GaiaAgent kan kallas som en funktion.Den kommer att anropa din process_task metod för att generera svaret."""print(f"Agent received question (first 50 chars): {question[:50]}...")result = self.process_task(question)print(f"Agent returning answer: {result[:100]}...") # För att inte fylla loggarna med för långa svarreturn result# --- END OF MISSING METHOD ---def process_task(self, task_description: str) -> str:# Instruction to the LLM to perform the task and use tools.# We need to build a prompt that instructs the model to use tools.prompt = f"""You are a helpful and expert AI assistant with access to a search tool.Your task is to carefully and accurately answer questions by using the search tool when necessary.Always provide a complete and correct answer based on the information you find.You must follow a Thought, Tool, Observation, Answer (TTOA) pattern.**Thought:** First, carefully consider the task. What information do you need to answer the question? Do you need to use a tool?**Tool:** If you need to search, use the search_tavily tool. The format is: <TOOL_CODE>search_tavily("your search query")</TOOL_CODE>**Observation:** After a tool call, you will receive an observation (the tool's output). This is factual information.**Answer:** Once you have gathered all necessary information, provide your final, concise answer directly.Your available tools:1. search_tavily(query: str): Searches on Tavily and returns relevant results.Example Interaction:Task: What is the capital of France?Thought: I need to find the capital of France. I should use the search_tavily tool.Tool: <TOOL_CODE>search_tavily("capital of France")</TOOL_CODE>Observation: The capital of France is Paris.Answer: The capital of France is Paris.Now, let's start.Task: {task_description}"""max_iterations = 3current_response_history = "" # Ny variabel för att bygga upp historikenfor i in range(max_iterations):# Lägg till "Thought:" här för att uppmuntra modellen att starta sin tankeprocessfull_prompt = prompt + current_response_history + "\n\nThought:"print(f"[{i+1}/{max_iterations}] Generating response with prompt length: {len(full_prompt)}")generated_text = self.text_generator(full_prompt,max_new_tokens=1024, # Fortsätt med 1024 eller öka till 2048num_return_sequences=1,pad_token_id=self.tokenizer.eos_token_id,do_sample=True,top_k=50, top_p=0.95,temperature=0.7)[0]['generated_text']# Extrahera endast den nya delen av texten (modellens respons efter den sista "Thought:")new_content = generated_text[len(full_prompt):].strip()print(f"DEBUG - Full generated_text: \n---START---\n{generated_text}\n---END---")print(f"DEBUG - Extracted new_content: '{new_content}'")# Kontrollera om modellen genererade ett svar som en 'Answer:'if "Answer:" in new_content:final_answer = new_content.split("Answer:", 1)[1].strip()print(f"Final answer from model:\n{final_answer}")return final_answer # Returnera det slutgiltiga svaretelif "<TOOL_CODE>" in new_content and "</TOOL_CODE>" in new_content:# Modellen genererade ett verktygskall.# Vi vill inte inkludera modellens egna "Observation:" eller "Tool:"-text i historiken# innan verktyget faktiskt körts. Vi tar bara själva tool_code strängen.tool_call_start = new_content.find("<TOOL_CODE>")tool_call_end = new_content.find("</TOOL_CODE>") + len("</TOOL_CODE>")# Försök att extrahera tanken som ledde till verktygskalletthought_part = ""if "Thought:" in new_content[:tool_call_start]:thought_part = new_content.split("Thought:", 1)[1].split("Tool:", 1)[0].strip()elif tool_call_start > 0: # Om det finns text före tool codethought_part = new_content[:tool_call_start].strip()tool_code_section = new_content[tool_call_start:tool_call_end]tool_call_str = tool_code_section.replace("<TOOL_CODE>", "").replace("</TOOL_CODE>", "").strip()print(f"Tool call detected: {tool_call_str}")try:if tool_call_str.startswith("search_tavily("):query = tool_call_str[len("search_tavily("):-1].strip().strip('"').strip("'")tool_output = search_tavily(query)print(f"Tool result: {tool_output[:200]}...")# Lägg till tanken, verktygskallet och det FAKTISKA observationen till historikencurrent_response_history += f"\n\nThought: {thought_part}\nTool: {tool_code_section}\nObservation: {tool_output}\n"else:tool_output = f"Unknown tool: {tool_call_str}"print(f"Error: {tool_output}")current_response_history += f"\n\nThought: {thought_part}\nTool: {tool_code_section}\nObservation: {tool_output}\n"except Exception as tool_e:tool_output = f"Error running tool {tool_call_str}: {tool_e}"print(f"Error: {tool_output}")current_response_history += f"\n\nThought: {thought_part}\nTool: {tool_code_section}\nObservation: {tool_output}\n"else:# Modellen genererade varken ett verktygskall eller ett slutgiltigt svar.# Lägg till det den faktiskt genererade till historiken så den kan fortsätta sin tanke.current_response_history += f"\n\nThought: {new_content}\n"print(f"Model generated non-tool/non-answer content. Appending: {new_content[:100]}...")# Om max_iterations nås utan slutgiltigt svarreturn "Agent could not complete the task within the allowed iterations. Latest relevant content: " + \(current_response_history[-500:] if current_response_history else "No meaningful content generated.")12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import osimport torchfrom huggingface_hub import InferenceClient# Importera smolagents komponenterfrom smolagents import ToolCallingAgent, Tool, InferenceClientModel# Importera dina befintliga, anpassade verktygsfunktioner# Se till att dessa filer (tavily_search.py, asr_tool.py, etc.) finns i samma katalog eller är korrekt importerbara.from tavily_search import search_tavilyfrom asr_tool import transcribe_audiofrom excel_tool import analyze_excelfrom math_tool import calculate_mathclass GaiaAgent:def __init__(self, model_id: str = "google/gemma-2b-it"):"""Initialiserar GaiaAgent, som nu använder smolagents.ToolCallingAgent internt."""print(f"Initialiserar GaiaAgent med modell: {model_id}")# Säkerställ att Hugging Face token är tillgänglig från miljönhf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN")if not hf_token:raise ValueError("Hugging Face token (HF_TOKEN eller HUGGING_FACE_HUB_TOKEN) är inte konfigurerad i miljövariabler.""Vänligen lägg till din token som en 'Repository secret' i dina Space-inställningar.")# Initialisera InferenceClientModel för att kommunicera med Hugging Face Inference APItry:# InferenceClientModel ansluter till en fjärrmodell, så lokal laddning av tokenizer/modell tas bort.self.llm_model = InferenceClientModel(model=InferenceClient(model=model_id, token=hf_token))print("InferenceClientModel laddad framgångsrikt.")except Exception as e:raise RuntimeError(f"Misslyckades att initialisera InferenceClientModel: {e}.""Vänligen kontrollera din HF token och att modellen är tillgänglig/laddningsbar.")# Definiera dina anpassade verktyg som smolagents.Tool objekt# Detta gör dem kompatibla med ToolCallingAgent.tools_list = [Tool(name="search_tavily",description="Användbart för att söka information online med Tavily Search. Returnerar en sammanfattning av de mest relevanta resultaten från webben. Kräver en fråga som input.",function=search_tavily),Tool(name="transcribe_audio",description="Transkriberar ljudfil till text. Användbart för att omvandla tal till text från en angiven ljudfilsväg. Kräver en filsökväg till ljudfilen som input.",function=transcribe_audio),Tool(name="analyze_excel",description="Analysera Excel-filer och returnera detaljerad information om rader, kolumner, datatyper och statistik (summa, medelvärde, max, min för numeriska kolumner). Kan ta både en lokal filväg eller en URL till Excel-filen som input.",function=analyze_excel),Tool(name="calculate_math",description="Beräkna matematiska uttryck. Användbart för att utföra aritmetiska operationer som addition, subtraktion, multiplikation, division och potenser. Tar ett matematiskt uttryck som en sträng som input.",function=calculate_math)]print(f"Laddade {len(tools_list)} anpassade verktyg.")# Initialisera ToolCallingAgent med dina verktyg och din modellself.agent = ToolCallingAgent(tools=tools_list,model=self.llm_model,verbosity_level=2 # Ställ in detaljnivå för att se agentens tankeprocess)print("ToolCallingAgent initialiserad.")def process_task(self, task_prompt: str) -> str:"""Bearbetar en uppgift med den interna ToolCallingAgent.Denna metod ersätter den tidigare manuella verktygsexekveringsloopen."""print(f"\nBearbetar uppgift med ToolCallingAgent: '{task_prompt}'")try:final_answer = self.agent.run(task_prompt)print(f"\nToolCallingAgent avslutad. Slutgiltigt svar: {final_answer}")return final_answerexcept Exception as e:error_message = f"Ett fel uppstod under agentens bearbetning: {e}"print(error_message)return f"Agenten kunde inte slutföra uppgiften på grund av ett fel: {error_message}"
agent.py+79-147