Spaces:
Sleeping
Sleeping
Update agent.py
Browse files58 unmodified lines596061626364656667686970717273747576777879801 unmodified line8283848586878889909192939495969798991001011021031041051065 unmodified lines11211311411511611711811912012112212312412512612712812958 unmodified linesYour 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.Your available tools:1. search_tavily(query: str): Searches on Tavily and returns relevant results.Use this tool to find information on the internet that you don't know or need to verify.To use a tool, write it in the following exact format:<TOOL_CODE>tool_name("your search query")</TOOL_CODE>Example:If you need to know the capital of France:<TOOL_CODE>search_tavily("capital of France")</TOOL_CODE>When you have found all the necessary information and are ready to answer the task, provide your final answer.Task: {task_description}"""1 unmodified linecurrent_response = ""for i in range(max_iterations):full_prompt = prompt + current_response + "\n\nWhat is the next step or your final answer?"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, # Behold 1024 eller öka om behövsnum_return_sequences=1,pad_token_id=self.tokenizer.eos_token_id,do_sample=True,top_k=50, top_p=0.95,temperature=0.8 # Behold 0.8 eller justera vid behov)[0]['generated_text']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}'")if "<TOOL_CODE>" in new_content and "</TOOL_CODE>" in new_content:start_index = new_content.find("<TOOL_CODE>") + len("<TOOL_CODE>")end_index = new_content.find("</TOOL_CODE>")tool_call_str = new_content[start_index:end_index].strip()5 unmodified linesquery = tool_call_str[len("search_tavily("):-1].strip().strip('"').strip("'")tool_output = search_tavily(query)print(f"Tool result: {tool_output[:200]}...")current_response += f"\n\nTool Result from {tool_call_str}:\n{tool_output}\n"else:tool_output = f"Unknown tool: {tool_call_str}"print(f"Error: {tool_output}")current_response += f"\n\n{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 += f"\n\n{tool_output}\n"else:final_answer = new_contentprint(f"Final answer from model:\n{final_answer}")return final_answer.strip()return "Agent could not complete the task within the allowed iterations. Latest response: " + new_content.strip()58 unmodified lines596061626364656667686970717273747576777879808182831 unmodified line8586878889909192939495969798991001011021031041051061071081091101111121131141155 unmodified lines12112212312412512612712812913013113213313413513613713858 unmodified linesYour 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).**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}"""1 unmodified linecurrent_response = ""for i in range(max_iterations):# Lägg till "Thought:" här för att uppmuntra modellen att starta sin tankeprocessfull_prompt = prompt + current_response + "\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 # Justera temperaturen till 0.7)[0]['generated_text']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_answerelif "<TOOL_CODE>" in new_content and "</TOOL_CODE>" in new_content:start_index = new_content.find("<TOOL_CODE>") + len("<TOOL_CODE>")end_index = new_content.find("</TOOL_CODE>")tool_call_str = new_content[start_index:end_index].strip()5 unmodified linesquery = tool_call_str[len("search_tavily("):-1].strip().strip('"').strip("'")tool_output = search_tavily(query)print(f"Tool result: {tool_output[:200]}...")current_response += f"\n\nObservation: {tool_output}\n"else:tool_output = f"Unknown tool: {tool_call_str}"print(f"Error: {tool_output}")current_response += f"\n\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 += f"\n\nObservation: {tool_output}\n"else:# Om modellen varken ger svar eller verktygskall men genererar något annatcurrent_response += f"\n\n{new_content}\n"print(f"Model generated non-tool/non-answer content. Appending: {new_content[:100]}...")return "Agent could not complete the task within the allowed iterations. Latest response: " + new_content.strip() if new_content else "Agent could not complete the task within the allowed iterations. No meaningful content generated."
agent.py+34-25