Spaces:
Runtime error
Runtime error
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from pydantic import PrivateAttr | |
| from langchain_core.tools.base import BaseTool | |
| from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage | |
| from langchain_openai import ChatOpenAI | |
| import time | |
| from openai import OpenAI | |
| class AnswerQuestionTool(BaseTool): | |
| name : str = "answer_question_tool" | |
| description: str = "Use this tool to answer any elementary question that you can solve without needing access to any external tool. Simply provide the question in input, reporting the whole question including desired output format. You can use this tool for example for vegetable classification." | |
| _llm = PrivateAttr() | |
| _system_prompt = PrivateAttr() | |
| def __init__(self): | |
| super().__init__() | |
| #self._llm = ChatGoogleGenerativeAI( | |
| # model="gemini-2.0-flash", | |
| # temperature=0) | |
| #self._llm = ChatOpenAI(model="o4-mini", temperature=0) | |
| self._system_prompt = SystemMessage("""You are a helpful assistant. | |
| You will be given a question and you will have to answer that question. | |
| Provide also the reasoning for your answer as well as your final answer. | |
| When provided with a list you must stick with the exact terms provided in the list and not make any modification. | |
| Green beans, corn and zucchini are NOT VEGEATABLES BOTANICALLY! | |
| Let's think step by step. | |
| """) | |
| def _run(self, question: str) -> str: | |
| human_message = HumanMessage( | |
| content=[ | |
| {"type": "text", "text": question}, | |
| ] | |
| ) | |
| time.sleep(5) | |
| client = OpenAI() | |
| response = client.responses.create( | |
| model="o4-mini", | |
| messages = [ | |
| { | |
| "role": "system", "content": self._system_prompt.text() | |
| }, | |
| { | |
| "role": "user", "content": question | |
| }] | |
| ) | |
| #response = self._llm.invoke([self._system_prompt, human_message]) | |
| return response |