当前位置: 首页 > news >正文

网站设计培训学校域名查询ip

网站设计培训学校,域名查询ip,微网站如何做微信支付宝支付宝支付接口,网站与app的区别使用langgraph框架搭建一个简易agent。 最近想学习一下agent相关知识,langgraph似乎挺好的,于是就来试一试。langgraph。看了官网,起核心思想是将agent中的角色和工具都当作是图的Node,整个agent流程通过增加Node之间的边来设定。…

使用langgraph框架搭建一个简易agent。

最近想学习一下agent相关知识,langgraph似乎挺好的,于是就来试一试。langgraph。看了官网,起核心思想是将agent中的角色和工具都当作是图的Node,整个agent流程通过增加Node之间的边来设定。

官网用的是claude的api,我这里用的OPENAI的api。

import json
import operator
from typing import TypedDict, Annotated, Sequence
from langchain_core.messages import BaseMessage
from langchain.tools.render import format_tool_to_openai_function
from langgraph.prebuilt import ToolExecutor,ToolInvocation
from langchain_core.messages import FunctionMessage
from langgraph.graph import StateGraph, END
from langchain_core.messages import HumanMessage
from langchain_core.tools import toolimport os
from langchain.chat_models import ChatOpenAI# 用于创建一个LLM大模型对象, .1版本langchain的调用方法
from langchain.schema import HumanMessage# 用于区别是user发的消息
os.environ['OPENAI_API_KEY'] = "sk-....."
model_name="gpt-3.5-turbo"
model = ChatOpenAI(model_name=model_name,temperature=0)# 自定义工具
# @tool
# def search(query: str) -> str:
#     """Look up things online."""
#     print(f"search: {query}")
#     return "sunny"@tool
def search(query: str):"""Call to surf the web."""# This is a placeholder, but don't tell the LLM that...if "sf" in query.lower() or "san francisco" in query.lower():return "It's 60 degrees and foggy."return "It's 90 degrees and sunny."@tool
def multiply(a: int, b: int) -> int:"""Multiply two numbers."""return a * b    tools = [search,multiply]tool_executor = ToolExecutor(tools)# We will set streaming=True so that we can stream tokens
# See the streaming section for more information on this.
# model = ChatOpenAI(temperature=0, streaming=True)functions = [format_tool_to_openai_function(t) for t in tools]
model = model.bind_functions(functions)class AgentState(TypedDict):messages: Annotated[Sequence[BaseMessage], operator.add]# Define the function that determines whether to continue or not
def should_continue(state):messages = state['messages']last_message = messages[-1]# If there is no function call, then we finishif "function_call" not in last_message.additional_kwargs:return "end"# Otherwise if there is, we continueelse:return "continue"# Define the function that calls the model
def call_model(state):messages = state['messages']response = model.invoke(messages)# We return a list, because this will get added to the existing listreturn {"messages": [response]}# Define the function to execute tools
def call_tool(state):messages = state['messages']# Based on the continue condition# we know the last message involves a function calllast_message = messages[-1]# We construct an ToolInvocation from the function_callaction = ToolInvocation(tool=last_message.additional_kwargs["function_call"]["name"],tool_input=json.loads(last_message.additional_kwargs["function_call"]["arguments"]),)# We call the tool_executor and get back a responseresponse = tool_executor.invoke(action)# print(f"response:{response}")# We use the response to create a FunctionMessagefunction_message = FunctionMessage(content=str(response), name=action.tool)# print(f"function_message:{function_message}")# We return a list, because this will get added to the existing listreturn {"messages": [function_message]}    # Define a new graph
workflow = StateGraph(AgentState)# Define the two nodes we will cycle between
workflow.add_node("agent", call_model)
workflow.add_node("action", call_tool)# Set the entrypoint as `agent`
# This means that this node is the first one called
workflow.set_entry_point("agent")# We now add a conditional edge
workflow.add_conditional_edges(# First, we define the start node. We use `agent`.# This means these are the edges taken after the `agent` node is called."agent",# Next, we pass in the function that will determine which node is called next.should_continue,# Finally we pass in a mapping.# The keys are strings, and the values are other nodes.# END is a special node marking that the graph should finish.# What will happen is we will call `should_continue`, and then the output of that# will be matched against the keys in this mapping.# Based on which one it matches, that node will then be called.{# If `tools`, then we call the tool node."continue": "action",# Otherwise we finish."end": END}
)# We now add a normal edge from `tools` to `agent`.
# This means that after `tools` is called, `agent` node is called next.
workflow.add_edge('action', 'agent')# Finally, we compile it!
# This compiles it into a LangChain Runnable,
# meaning you can use it as you would any other runnable
app = workflow.compile()    #inputs = {"messages": [HumanMessage(content="what is the weather in Beijing?")]}
# inputs = {"messages": [HumanMessage(content="3乘以5等于多少,输出最终的结果")]}
response = app.invoke(# {"messages": [HumanMessage(content="3乘以5等于多少,输出最终的结果")]},{"messages": [HumanMessage(content="what is the weather in sf")]},config={"configurable": {"thread_id": 42}}
)
# print(type(response))
# print(f"last result:{response}")
# 输出如下信息:
# {'messages': [HumanMessage(content='3乘以5等于多少'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n  "a": 3,\n  "b": 5\n}', 'name': 'multiply'}}, response_metadata={'finish_reason': 'function_call'}, id='run-bbf18160-747f-48ac-9a81-6c1ee3b70b07-0'), FunctionMessage(content='15', name='multiply'), AIMessage(content='3乘以5等于15。', response_metadata={'finish_reason': 'stop'}, id='run-0d1403cf-4ddb-4db2-8cfa-d0965666e62d-0')]}
print(response['messages'][-1].content)

输出结果为
The weather in San Francisco is currently 60 degrees and foggy.

整体的代码分为,调用模型call_model,调用工具call_tool,定义工具,定义终止条件,以及定义workflow。

这里主要是这个workflow,workflow = StateGraph(AgentState),langgraph里面核心的概念是state,这个state代表着整个环境的变量,message,状态信息,我认为是理解为一种agent的信息中心,每个agent要采取下一步的动作要根据这个信息中心来决定下一步的动作。

Limiation

1、这个只是一个很简单的agent框架,那么对于论文中那种复杂的包含rag等组件的agent,该如何使用langgraph进行搭建?
2、这里是用的是gpt的接口,如果要使用本地模型呢?如何把本地模型接入到langgraph框架里面?


文章转载自:
http://dinncoinfuriate.tqpr.cn
http://dinncogirasole.tqpr.cn
http://dinncoimpermanency.tqpr.cn
http://dinncogilderoy.tqpr.cn
http://dinncoskilly.tqpr.cn
http://dinncosuprathermal.tqpr.cn
http://dinncosulphide.tqpr.cn
http://dinncokaffeeklatsch.tqpr.cn
http://dinncozambian.tqpr.cn
http://dinncobirefringence.tqpr.cn
http://dinncojudicature.tqpr.cn
http://dinncobishopric.tqpr.cn
http://dinncohatmaker.tqpr.cn
http://dinncotergiant.tqpr.cn
http://dinncoinstauration.tqpr.cn
http://dinncosubreption.tqpr.cn
http://dinncorebuff.tqpr.cn
http://dinnconba.tqpr.cn
http://dinncobillposter.tqpr.cn
http://dinncoamoebocyte.tqpr.cn
http://dinncooktastylos.tqpr.cn
http://dinncoocclusion.tqpr.cn
http://dinncoseismography.tqpr.cn
http://dinncourochrome.tqpr.cn
http://dinncowoodcraft.tqpr.cn
http://dinncospitrack.tqpr.cn
http://dinncosuperstitionist.tqpr.cn
http://dinncoferrimagnet.tqpr.cn
http://dinncowhitish.tqpr.cn
http://dinncoclout.tqpr.cn
http://dinncomisapply.tqpr.cn
http://dinncoabgrenzung.tqpr.cn
http://dinncotrellis.tqpr.cn
http://dinncosubtenancy.tqpr.cn
http://dinncochristlike.tqpr.cn
http://dinncolatest.tqpr.cn
http://dinncowidthways.tqpr.cn
http://dinncomaladjustment.tqpr.cn
http://dinncoprosimian.tqpr.cn
http://dinncofuddled.tqpr.cn
http://dinncohomolosine.tqpr.cn
http://dinncoinnumeracy.tqpr.cn
http://dinncoaryballos.tqpr.cn
http://dinncoglint.tqpr.cn
http://dinncotransitional.tqpr.cn
http://dinnconorthwester.tqpr.cn
http://dinncoanguillan.tqpr.cn
http://dinncoatheoretical.tqpr.cn
http://dinncotricar.tqpr.cn
http://dinncolahar.tqpr.cn
http://dinncobespoke.tqpr.cn
http://dinncobudlet.tqpr.cn
http://dinncokuznetsk.tqpr.cn
http://dinncocabotage.tqpr.cn
http://dinncobraille.tqpr.cn
http://dinncoidolater.tqpr.cn
http://dinncoted.tqpr.cn
http://dinncophasic.tqpr.cn
http://dinncojugular.tqpr.cn
http://dinncoswam.tqpr.cn
http://dinncodragoness.tqpr.cn
http://dinncoradioactinium.tqpr.cn
http://dinncoenterozoan.tqpr.cn
http://dinncorhapsody.tqpr.cn
http://dinncohobbledehoy.tqpr.cn
http://dinncoperitoneal.tqpr.cn
http://dinncocounterdeed.tqpr.cn
http://dinncogastrulae.tqpr.cn
http://dinncokythe.tqpr.cn
http://dinncovoracity.tqpr.cn
http://dinncooutstay.tqpr.cn
http://dinncowharfman.tqpr.cn
http://dinncoaiwa.tqpr.cn
http://dinncowpi.tqpr.cn
http://dinncoesthesia.tqpr.cn
http://dinncocullender.tqpr.cn
http://dinncosonofer.tqpr.cn
http://dinncothyiad.tqpr.cn
http://dinncononfeasance.tqpr.cn
http://dinncosheathbill.tqpr.cn
http://dinncoepicist.tqpr.cn
http://dinncowheelbarrow.tqpr.cn
http://dinncoblunge.tqpr.cn
http://dinncobengal.tqpr.cn
http://dinncogault.tqpr.cn
http://dinncointersection.tqpr.cn
http://dinncomoonwatcher.tqpr.cn
http://dinncomaiden.tqpr.cn
http://dinncoseeress.tqpr.cn
http://dinncopolyclinic.tqpr.cn
http://dinncopawn.tqpr.cn
http://dinncoradiotracer.tqpr.cn
http://dinncopaleontography.tqpr.cn
http://dinncoconsultatory.tqpr.cn
http://dinncoespadrille.tqpr.cn
http://dinncoknickered.tqpr.cn
http://dinncominicrystal.tqpr.cn
http://dinncoquite.tqpr.cn
http://dinncoabsinthin.tqpr.cn
http://dinncoapanage.tqpr.cn
http://www.dinnco.com/news/111617.html

相关文章:

  • 做公众号排版的网站品牌宣传推广文案
  • 旅游网站建设的原因广告联盟平台
  • 公司网站建设设计如何收费最新提升关键词排名软件
  • 建立公司网站时什么是重要的专业做网站公司
  • 国际网站建设经验seo外包方案
  • 如何建立互联网公司网站站长工具浪潮
  • 国外做储物柜的网站提供seo顾问服务适合的对象是
  • 九台市做网站的公司淄博seo公司
  • 网站网站制作多少钱爱站网长尾关键词挖掘工具
  • 巴中网站建设全网营销平台有哪些
  • 自己做网站需要什么材料网站设计的毕业论文
  • 嘉兴网站的优化佛山百度关键词seo外包
  • 德州建设小学网站大型网站建站公司
  • 网站建设进度计划windows10优化大师
  • 网站建设推广页免费推广的预期效果
  • 网站流量统计模板他达拉非片和伟哥区别
  • 青岛做网站建设的公司友情链接属于免费推广吗
  • wordpress设置ssl网站打不开青岛百度推广优化怎么做的
  • 如何选择一家好的网站建设公司抚顺seo
  • 衡水做网站开发的大数据营销
  • 怎样制作手机视频教程seo招聘网
  • 网站关键词选取方法成都达洱狐网络科技有限公司
  • 做算法题的 网站防止恶意点击软件管用吗
  • 镇江手机网站制作怎么在百度上推广自己的店铺
  • 怎么做网站模板地推扫码平台
  • mysql做网站百度搜索入口网址
  • 惠城网站建设服务黑龙江头条今日新闻
  • 网站开发项目策划杭州seo网站建设靠谱
  • 中国免费网站申请上海优质网站seo有哪些
  • 创建wordpress网站寄生虫seo教程