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

杭州高瑞网站建设知名网络营销推广

杭州高瑞网站建设,知名网络营销推广,济南建设招标投标协会,网站要怎么做才专业1. 导入必要的库 首先,我们需要导入所需的库。这里我们使用 langchain_openai 来与 智谱AI 模型进行交互,并使用 langchain_core.tools 来定义自定义工具。 from langchain_openai import ChatOpenAI from typing import Literal from langchain_core.…

1. 导入必要的库

首先,我们需要导入所需的库。这里我们使用 langchain_openai 来与 智谱AI 模型进行交互,并使用 langchain_core.tools 来定义自定义工具。

from langchain_openai import ChatOpenAI
from typing import Literal
from langchain_core.tools import tool

2. 初始化 智谱AI 模型

接下来,我们初始化 智谱AI 模型。这里需要指定模型的参数,包括温度、模型名称、API 密钥和 API 基地址。

model = ChatOpenAI(temperature=0,model="glm-4-plus",openai_api_key="your_api_key",openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
)

3. 定义自定义工具

我们定义一个获取天气信息的工具 get_weather,该工具接受一个城市名称作为参数,并返回相应的天气信息。

@tool
def get_weather(city: Literal["nyc", "sf"]):"""Use this to get weather information."""if city == "nyc":return "It might be cloudy in nyc"elif city == "sf":return "It's always sunny in sf"else:raise AssertionError("Unknown city")

4. 创建工具列表

将定义好的工具添加到工具列表中。

tools = [get_weather]

5. 初始化记忆存储

使用 MemorySaver 来存储对话过程中的状态。

from langgraph.checkpoint.memory import MemorySavermemory = MemorySaver()

6. 创建反应式代理

使用 create_react_agent 函数创建一个反应式代理,该代理将模型和工具结合在一起。

from langgraph.prebuilt import create_react_agentgraph = create_react_agent(model, tools=tools, checkpointer=memory)

7. 定义打印流函数

为了更好地展示对话过程,我们定义一个 print_stream 函数,用于打印流式输出的消息。

def print_stream(stream):for s in stream:message = s["messages"][-1]if isinstance(message, tuple):print(message)else:message.pretty_print()

8. 发送查询并打印结果

配置输入参数,并发送查询请求,最后打印结果。

config = {"configurable": {"thread_id": "1"}}
inputs = {"messages": [("user", "What's the weather in NYC?")]}print_stream(graph.stream(inputs, config=config, stream_mode="values"))

输出结果如下:

==============================[1m Human Message [0m=================================What's the weather in NYC?
================================[1m Ai Message [0m==================================
Tool Calls:get_weather (call_9208192282885233822)Call ID: call_9208192282885233822Args:city: nyc
================================[1m Tool Message [0m=================================
Name: get_weatherIt might be cloudy in nyc
================================[1m Ai Message [0m==================================It might be cloudy in NYC.

9. 发送另一个查询并打印结果

再次配置输入参数,发送新的查询请求,并打印结果。

inputs = {"messages": [("user", "What's it known for?")]}
print_stream(graph.stream(inputs, config=config, stream_mode="values"))

输出结果如下:

==============================[1m Human Message [0m=================================What's it known for?
================================[1m Ai Message [0m==================================New York City is known for a multitude of things, making it one of the most iconic and influential cities in the world. Here are some highlights:1. **Statue of Liberty**: A symbol of freedom and democracy, this iconic statue is located on Liberty Island.2. **Central Park**: An expansive urban park in the heart of Manhattan, known for its lush landscapes, recreational activities, and cultural events.3. **Broadway**: The pinnacle of American theater, Broadway is famous for its high-quality performances and historic theaters.4. **Times Square**: A bustling commercial intersection known for its bright lights, massive digital billboards, and the annual New Year's Eve ball drop.5. **Empire State Building**: An Art Deco skyscraper that once held the title of the world's tallest building and remains a symbol of New York's skyline.6. **Museums and Art**: Home to world-renowned institutions like the Metropolitan Museum of Art, the Museum of Modern Art (MoMA), and the Guggenheim.7. **Diverse Cuisine**: Reflecting its status as a melting pot, NYC offers an incredible variety of global cuisines.8. **Financial Hub**: Wall Street and the New York Stock Exchange are central to the global economy.9. **Fashion Capital**: Known for its influence on global fashion trends, hosting events like New York Fashion Week.10. **Cultural Diversity**: A city of immigrants, NYC boasts a rich tapestry of cultures, languages, and traditions.These are just a few aspects that contribute to NYC's global reputation and appeal.

参考链接:https://langchain-ai.github.io/langgraph/how-tos/create-react-agent-memory/

http://www.dinnco.com/news/50724.html

相关文章:

  • 给网站首页图片做外网超链接_为什么会弹出一个服务器登录窗口外贸谷歌seo
  • 嘉定西安网站建设投放广告找什么平台
  • wordpress出不来安装界面东莞seo网站优化排名
  • 省企联网站建设要求厦门百度广告
  • 宿迁做网站的公司如何做网络营销推广
  • 舟山城乡建设培训中心网站百度推广账号登录
  • 个人做淘宝客网站有哪些打广告的免费软件
  • 高端网站建设苏州网络推广方案设计
  • 上海做网站设计公司网络公司网站
  • 男男互做网站怎样进行seo推广
  • 新开传奇手游发布网站seo优化教程培训
  • 做彩网站有哪些燕郊今日头条
  • 个人网站开发与实现开题报告制作一个简单的网站
  • 溆浦县建筑公司网站电商运营是做什么的
  • 做网站的困难百度关键词优化公司哪家好
  • 已有域名 做网站如何在百度发布信息推广
  • 快速免费建网站环球资源网官方网站
  • 没有网站怎么做淘客成人编程培训机构排名前十
  • 群晖 多个wordpress广告网站建设网站排名优化
  • 百度云可以做网站吗百中搜
  • 网站维护中 源码优化大师卸载不了
  • 专门做杂志的网站今日头条热榜
  • 太原疫情最新情况静默公告墨子学院seo
  • 连云港网站建设wang公众号排名优化软件
  • 做外包装很厉害的网站石家庄疫情最新消息
  • wordpress readd安徽seo网络优化师
  • 车牌照损坏在网站做的能用吗网店运营公司
  • 营销型网站的缺点百度快速排名优化服务
  • 推广策划案优化大师免费版
  • 网站架构 seo个人如何注册网址