【无标题】

、Langchain在智谱清言的使用

Langchain基础例子

下而的功能都是智谱清言API来验证

所有的前提都需要先到智谱先申请一个APIKEY

地址:https://open.bigmodel.cn/usercenter/proj-mgmt/apikeys

下面是实操过程

  1. 安装依赖库

    pip install -U langchain-openai
    pip install openai

  2. 初始LLM对象模型

    from langchain_openai import ChatOpenAI
    api_key = "xxxx" # 这里为你前面申请到的API
    llm = ChatOpenAI(
    temperature=0.95,
    model="glm-4-flash",
    openai_api_key=api_key,
    openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
    )

  3. prompt模板的使用

    from langchain_core.prompts import ChatPromptTemplate
    prompt = ChatPromptTemplate.from_messages(
    [
    ("system", "你是世界级的技术专家"),
    ("user", "{input}"),
    ]
    )

  4. 初始一个chain

    output_parser = StrOutputParser() # 字符解析器
    chain = prompt|llm |output_parser
    result = chain.invoke({"input":"帮我写一篇关于AI的技术文章,100个字"})
    print(result)

    如果不使用StrOutputParser的话输出的是一个对象,如果的话会输出一个字符串

完整内容

复制代码
content='人工智能技术正飞速发展,从语音识别到自动驾驶,从医疗诊断到金融服务,AI正改变着我们的生活。我国在AI领域持续发力,推动产业升级,助力国家战略发展。' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 42, 'prompt_tokens': 23, 'total_tokens': 65, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'glm-4-flash', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-b378b396-e5b0-4a96-ba6f-f73d3741104b-0' usage_metadata={'input_tokens': 23, 'output_tokens': 42, 'total_tokens': 65, 'input_token_details': {}, 'output_token_details': {}}
相关推荐
shut up1 天前
LangChain - 如何使用阿里云百炼平台的Qwen-plus模型构建一个桌面文件查询AI助手 - 超详细
人工智能·python·langchain·智能体
SuperHeroWu71 天前
【HarmonyOS AI赋能】朗读控件详解
华为·ai·harmonyos·朗读·赋能·speechkit·场景化语音
liliangcsdn1 天前
如何基于ElasticsearchRetriever构建RAG系统
大数据·elasticsearch·langchain
东方佑1 天前
基于FastAPI与LangChain的Excel智能数据分析API开发实践
langchain·excel·fastapi
阿里-于怀1 天前
阿里云发布《AI 原生应用架构白皮书》
人工智能·阿里云·ai·架构·白皮书·ai原生
Elastic 中国社区官方博客1 天前
Simple MCP Client - 连接到 Elasticsearch MCP 并进行自然语言搜索
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2025-10-12)
ai·github·开源项目·github热榜
CoderJia程序员甲2 天前
GitHub 热榜项目 - 日榜(2025-10-13)
ai·开源·大模型·github·ai教程
Qiuner2 天前
历劫波,明真我——Debug Commune
ai·开源·bug·debug·信息差·信息·交流
武子康2 天前
AI-调查研究-102-具身智能 智能机械臂、自动驾驶与人形机器人的模仿学习、强化学习与多模态融合趋势
人工智能·深度学习·机器学习·ai·机器人·强化学习·具身智能