Spaces:
Sleeping
Sleeping
auth
Browse filesFiles changed (3)
- config.py+7-10
- hf_agent.py+62-15
- test_agent.pyAdded+42
18 unmodified lines192021222324252627282930313233343536373839404118 unmodified lines# --- System Prompts ---SYSTEM_PROMPTS = {"default": ("You are a helpful, concise, and robust AI assistant. ""You can use tools, analyze files, and answer any question. ""Always provide short, accurate, and well-structured answers. ""If a file is provided, use its content as context."),"math_code": ("You are an expert code interpreter and mathematician. ""Solve the user's question step by step. ""Show your reasoning and keep answers concise."),"retrieval": ("You are a retrieval-augmented AI assistant. ""Use the provided context to answer the question accurately."),"evaluation": ("You are participating in the GAIA evaluation. ""Provide accurate, comprehensive, and concise answers using all available tools and context.")}18 unmodified lines192021222324252627282930313233343536373818 unmodified lines# --- System Prompts ---SYSTEM_PROMPTS = {"default": ("You are a helpful AI assistant. Answer the user's question directly and accurately. ""Be concise and specific. Do not introduce yourself or explain what you are."),"math_code": ("You are a math and coding expert. Solve the problem step by step. ""Show your work and provide the final answer clearly."),"retrieval": ("Answer the question using only the provided context. ""Be specific and cite relevant information from the context."),"evaluation": ("You are participating in the GAIA evaluation. ""Provide accurate, comprehensive, and concise answers. Focus on the task at hand.")}
42 unmodified lines4344454647484950515253545556575859606162636442 unmodified lineskeywords = ["calculate", "compute", "solve", "equation", "math", "code", "python", "step by step", "formula", "percentage", "sum", "average", "mean", "median", "algebra", "geometry", "calculus"]return any(k in question.lower() for k in keywords)def __call__(self, question, files=None):context = ""if files:context = self.extract_file_context(files)if context:prompt = get_default_prompt("retrieval_question").format(context=context, question=question)system_prompt = get_system_prompt("retrieval")input_text = f"{system_prompt}\n{prompt}"return self.llm(input_text)[0]["generated_text"].strip()elif self.is_math_or_code(question):system_prompt = get_system_prompt("math_code")prompt = get_default_prompt("math_code_question").format(question=question)input_text = f"{system_prompt}\n{prompt}"return self.code(input_text)[0]["generated_text"].strip()else:system_prompt = get_system_prompt("default")prompt = get_default_prompt("direct_question").format(question=question)input_text = f"{system_prompt}\n{prompt}"return self.llm(input_text)[0]["generated_text"].strip()42 unmodified lines43444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011142 unmodified lineskeywords = ["calculate", "compute", "solve", "equation", "math", "code", "python", "step by step", "formula", "percentage", "sum", "average", "mean", "median", "algebra", "geometry", "calculus"]return any(k in question.lower() for k in keywords)def clean_response(self, generated_text, input_text):"""Clean the generated response by removing the input prompt"""# Remove the input text from the beginning if presentif generated_text.startswith(input_text):response = generated_text[len(input_text):].strip()else:response = generated_text.strip()# Remove common unwanted prefixesunwanted_prefixes = ["introduce yourself briefly.","i am a sophisticated ai language model","You are a helpful","You are an expert","You are a retrieval-augmented","<|assistant|>","Assistant:","Answer:","Response:"]for prefix in unwanted_prefixes:if response.lower().startswith(prefix.lower()):response = response[len(prefix):].strip()# If response is still the generic introduction, provide a fallbackif "sophisticated ai language model" in response.lower() or len(response) < 10:return "I'm ready to help you with your question. Could you please provide more specific details about what you'd like to know?"return responsedef __call__(self, question, files=None):context = ""if files:context = self.extract_file_context(files)try:if context:# Handle questions with file contextsystem_prompt = get_system_prompt("retrieval")prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"input_text = f"{system_prompt}\n{prompt}"result = self.llm(input_text, do_sample=True, temperature=0.7, pad_token_id=self.llm.tokenizer.eos_token_id)generated_text = result[0]["generated_text"]return self.clean_response(generated_text, input_text)elif self.is_math_or_code(question):# Handle math/code questionsprompt = f"Question: {question}\nSolution:"result = self.code(prompt, do_sample=True, temperature=0.3)generated_text = result[0]["generated_text"]return self.clean_response(generated_text, prompt)else:# Handle general questionssystem_prompt = get_system_prompt("default")prompt = f"Question: {question}\nAnswer:"input_text = f"{system_prompt}\n{prompt}"result = self.llm(input_text, do_sample=True, temperature=0.7, pad_token_id=self.llm.tokenizer.eos_token_id)generated_text = result[0]["generated_text"]return self.clean_response(generated_text, input_text)except Exception as e:return f"I encountered an error while processing your question: {str(e)}. Please try rephrasing your question."
123456789101112131415161718192021222324252627282930313233343536373839404142#!/usr/bin/env python3"""Test script to verify HuggingFaceAgent functionality"""
from hf_agent import HuggingFaceAgent
def test_agent(): print("Testing HuggingFace Agent...") agent = HuggingFaceAgent() # Test cases test_questions = [ "What is the capital of France?", "How do you calculate the area of a circle?", "Explain photosynthesis in simple terms", "What is 2 + 2?", "Tell me about machine learning" ] for i, question in enumerate(test_questions, 1): print(f"\n--- Test {i} ---") print(f"Question: {question}") try: answer = agent(question) print(f"Answer: {answer}") print(f"Length: {len(answer)} characters") # Check if it's the problematic generic response if "sophisticated ai language model" in answer.lower(): print("❌ Still returning generic introduction!") else: print("✅ Proper response received") except Exception as e: print(f"❌ Error: {e}") print("\n" + "="*50) print("Agent testing completed!")
if __name__ == "__main__": test_agent() config.py+7-10
hf_agent.py+62-15
test_agent.pyAdded+42