公司手机网站效果图,网站建设远程培训,网络科技公司名称,网站企业建设libraryIO的链接#xff1a;https://libraries.io/pypi/langchain-decorators 来colab玩玩它的demo 感觉这确实是个挺好用的库 想到之前纯调prompt来控制输出格式的痛苦#xff0c;这个可太有效了 cool~ 最下面这个react的多智能体例子很好玩#xff0c;来看看… libraryIO的链接https://libraries.io/pypi/langchain-decorators 来colab玩玩它的demo 感觉这确实是个挺好用的库 想到之前纯调prompt来控制输出格式的痛苦这个可太有效了 cool~ 最下面这个react的多智能体例子很好玩来看看 from typing import List
from langchain_decorators import llm_prompt
from langchain.agents import load_tools
from langchain.tools.base import BaseTool
from textwrap import dedent
from langchain_decorators import PromptTypes
from langchain_decorators.output_parsers import JsonOutputParser
import jsontools load_tools([ llm-math], llmGlobalSettings.get_current_settings().default_llm)# you may, or may not use pydantic as your base class... totally up to you
class MultilingualAgent:def __init__(self, tools:List[BaseTool],result_language:strNone) - None:self.tools tools# we can refer to our field in all out promptsself.result_language result_languageself.agent_scratchpad # we initialize our scratchpadself.feedback # we initialize our feedback if we get some error# other settingsself.iterations10self.agent_format_instructions dedent(\# Reasoning... write your reasoning here ...# Tooljson{{tool: name of the tool to use,tool_input: the input for the tool}}# Observationoutput from the tool... repeat this # Reasoning, # Tool, # Observation sequence multiple times until you know the final answer, when you write:# Final answer... write the final answer )propertydef tools_description(self)-str: # we can refer to properties in out prompts tooreturn \n.join([f - {tool.name}: {tool.description} for tool in self.tools])# we defined prompt type here, which will make llm_prompt(prompt_typePromptTypes.AGENT_REASONING, output_parsermarkdown, stop_tokens[Observation], verboseTrue)def reason(self)-dict:The system prompt:prompt:systemYou are an assistant that uses reasoning and tools to help user. You use tools for the task the tool is designed to. Before answering the question and/or using the tool, you should write down the explanation. Here is the list of tools available:{tools_description}Use this format:{agent_format_instructions}{? in {result_language}?} here ...{?Make sure to write the final answer in in {result_language}!?} User question:prompt:user{question}Scratchpad:prompt:assistant{agent_scratchpad}prompt:user{feedback}returndef act(self, tool_name:str, tool_input:str)-str:tool next((tool for tool in self.tools if tool.name.lower()tool_name.lower()tool_name.lower()))if tool is None:self.feedback fTool {tool_name} is not available. Available tools are: {self.tools_description}returnelse:try:result tool.run(tool_input)except Exception as e:if self.feedback is not None:# weve already experienced an error, so we are not going to try forever... lets raise this oneraise eself.feedback fTool {tool_name} failed with error: {e}.\nLets fix it and try again.tool_instructions json.dumps({tool:tool.name, tool_input:tool_input})self.agent_scratchpad f# Tool\njson\n{tool_instructions}\n\n# Observation\n\nResult from tool {tool_name}:\n\t{result}\ndef run(self, question):for i in range(self.iterations):reasoning self.reason(questionquestion)if reasoning.get(Final answer) is not None:return reasoning.get(Final answer)else:tool_info reasoning.get(Tool)tool_name, tool_input (None, None)if tool_info:tool_info_parsed JsonOutputParser().parse(tool_info)tool_name tool_info_parsed.get(tool)tool_input tool_info_parsed.get(tool_input)if tool_name is None or tool_input is None:self.feedback Your response was not in the expected format. Please make sure to response in correct format:\n self.agent_format_instructions continueself.act(tool_name, tool_input)raise Exception(fFailed to answer the question after {self.iterations} iterations. Last result: {reasoning})agent MultilingualAgent(toolstools, result_languageGerman )result agent.run(What is the surface of a sphere with radius with diameter of 100km?)print(\n\nHere is the agents answer:, result)下面是输出 这个包确实能带来不少方便~