Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesFiles changed (2)
- src/agent.py+83-2
- src/utils.pyAdded+16
123415 unmodified lines2021222324252 unmodified lines28293031323313 unmodified lines4748495051523 unmodified lines56575859606162636465666768697071727331 unmodified lines105106107108109110111112113114import jsonfrom langchain_core.messages import SystemMessage, HumanMessagefrom langchain_core.rate_limiters import InMemoryRateLimiter15 unmodified linesmodel: str,tools: list,system_prompt_path: str,openai_api_key: str = None,langfuse_callback_handler: CallbackHandler = None):2 unmodified lines:param model: The OpenAI model to use.:param tools: List of tools the agent can use.:param system_prompt_path: Path to the system prompt file.:param openai_api_key: OpenAI API key for authentication.:param langfuse_callback_handler: Langfuse callback handler fortracking and logging interactions.13 unmodified lines)with open(system_prompt_path, "r") as file:self.system_prompt = file.read()self.tools = toolsif langfuse_callback_handler is not None:self.chat_model.callbacks = [langfuse_callback_handler]3 unmodified lines)self.graph = self.__build_graph()def __call__(self, question: str) -> tuple[str, str]:"""Reply to a question using the agent and return the agents full replywith reasoning included.:param question: The question to ask the agent.:return: The agent's response."""final_state = self.graph.invoke(input={"messages": [SystemMessage(content=self.system_prompt),HumanMessage(content=question)]},config={31 unmodified linesresponse = self.chat_model_with_tools.invoke(state["messages"])return {"messages": [response]}if __name__ == "__main__":import osfrom langchain_community.tools import DuckDuckGoSearchResultsfrom tools import multiply, add, subtract, divide, modulus12345615 unmodified lines222324252627282 unmodified lines3132333435363713 unmodified lines515253545556573 unmodified lines61626364656667686970717273747576777879808182838485868731 unmodified lines119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195import base64import jsonfrom os.path import joinfrom langchain_core.messages import SystemMessage, HumanMessagefrom langchain_core.rate_limiters import InMemoryRateLimiter15 unmodified linesmodel: str,tools: list,system_prompt_path: str,data_path: str,openai_api_key: str = None,langfuse_callback_handler: CallbackHandler = None):2 unmodified lines:param model: The OpenAI model to use.:param tools: List of tools the agent can use.:param system_prompt_path: Path to the system prompt file.:param data_path: Data to be used by the agent.:param openai_api_key: OpenAI API key for authentication.:param langfuse_callback_handler: Langfuse callback handler fortracking and logging interactions.13 unmodified lines)with open(system_prompt_path, "r") as file:self.system_prompt = file.read()self.data_path = data_pathself.tools = toolsif langfuse_callback_handler is not None:self.chat_model.callbacks = [langfuse_callback_handler]3 unmodified lines)self.graph = self.__build_graph()def __call__(self,question: str,question_file: str | None) -> tuple[str, str]:"""Reply to a question using the agent and return the agents full replywith reasoning included.:param question: The question to ask the agent.:param question_file: The file that comes with the question.:return: The agent's response."""human_message = self.__format_human_message(question=question,question_file=question_file)final_state = self.graph.invoke(input={"messages": [SystemMessage(content=self.system_prompt),human_message]},config={31 unmodified linesresponse = self.chat_model_with_tools.invoke(state["messages"])return {"messages": [response]}def __format_human_message(self,question: str,question_file: str | None) -> HumanMessage:"""Format the human message for the agent.:param question: The question to ask the agent.:param question_file: The file that comes with the question.:return: Formatted HumanMessage."""if question_file is None:human_message = HumanMessage(content=question)else:if '.png' in question_file:with open(join(self.data_path, question_file), "rb") as file:file_content = base64.b64encode(file.read()).\decode("utf-8")human_message = HumanMessage(content=[{'type': 'text','text': question},{'type': 'image','source_type': 'base64','data': file_content,"mime_type": "image/png"}])elif '.mp3' in question_file:with open(join(self.data_path, question_file), "rb") as file:file_content = base64.b64encode(file.read()).\decode("utf-8")human_message = HumanMessage(content=[{'type': 'text','text': question},{'type': 'audio','source_type': 'base64','data': file_content,'mime_type': 'audio/mp3'}])elif '.py' in question_file:with open(join(self.data_path, question_file), "r") as file:file_content = file.read()human_message = HumanMessage(content=[{'type': 'text','text': f'''{question}\n\nHere is the code:```python\n{file_content}\n```'''},])elif '.xlsx' in question_file:human_message = HumanMessage(content=question)return human_messageif __name__ == "__main__":import osfrom langchain_community.tools import DuckDuckGoSearchResultsfrom tools import multiply, add, subtract, divide, modulus
12345678910111213141516import requests
def download_file(url: str, local_filepath: str) -> None: """ Download a file from a URL and save it locally.
Args: url (str): The URL of the file to download. local_filename (str): The local path where the file will be saved. """ with requests.get(url, stream=True) as response: response.raise_for_status() with open(local_filepath, 'wb') as file: for chunk in response.iter_content(chunk_size=8192): file.write(chunk)src/agent.py+83-2
src/utils.pyAdded+16