【星海出品】Langchain Prompt template

Management prompt words

We can use this program to face students from different families. But now this program cannot communicate in Chinese.

URL: https://platform.openai.com/account/api-keys

  • LLMs:

这是一个语言模型,It lets input words and return sentences.

input is a list of ChatMessage.

output is a single ChatMessage.

Object role

human message:ChatMessage 来自人类/用户。

AIMessage: ChatMessage 来自AI/助手。

SystemMessage: 来自系统的ChatMessage。

  • Prompt Template

provide a description for Language Model, Control the output for Language Model

How to use OpenAI

OpenAI

STEP 1

Enviroment

bash 复制代码
pip install langchain-openai
STEP 2

Method one
OpenAI-api

python 复制代码
export OPENAI_API_KEY="your-api-key"

from langchain_openai import ChatOpenAI

llm = ChatOpenAI()

Method two

python 复制代码
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(api_key="...")
STEP 3
python 复制代码
llm.invoke("how can langsmith help with testing?")

one or two also support predict

python 复制代码
predict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) → str
llm.predict("hello!")
python 复制代码
a = int(0)
try:
    from langchain_core.messages import HumanMessage
    a = '1'
except:
    try:
        from langchain.schema import HumanMessage
        a = '2'
    except:
        a = '3'
finally:
    print(a)
python 复制代码
text = "what would be a good company name for a company that makes colorful socks?"
message = (HumanMessage(content=text))
llm.predict_messages("message")

We can also guide its response with a prompt template. Prompt templates convert raw user input to better input to the LLM.

python 复制代码
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a world class technical documentation writer."),
    ("user", "{input}")
])

chain = prompt | llm 
chain.invoke({"input": "how can langsmith help with testing?"})

还可以添加输出解析器

python 复制代码
from langchain_core.output_parsers import StrOutputParser

output_parser = StrOutputParser()

chain = prompt | llm | output_parser

chain.invoke({"input": "how can langsmith help with testing?"})

We can now invoke it and ask the same question. The answer will now be a string (rather than a ChatMessage).

LangChain-ChatGLM:基于本地知识库的问答

langChain-ChatGLM git

ChatGLM-6B 简介

是一个开源模型,支持中英双语对话模型,基于 General Language Model(GLM)架构,具有62亿参数。

checkpoint,训练数据增加英文指令微调数据以平衡中英文数据比例

自我认知、提纲写作、文案写作、信息抽取

微调

针对预先训练的语言模型,在特定任务的少量数据集上对其进行进一步训练。

当任务或域定义明确,并且有足够的标记数据可供训练时,通常使用微调过程。

提示词工程

涉及设计自然语言 提示 或 指令,可以指导语言模型执行特定任务。

最适合需要 高精度 和 明确输出 的任务。提示工程可用于make引发所需输出的查询。

LangChain 简介

LangChain是一个用于开发语言模型驱动的应用程序的框架。

主要功能:
  • 调用语言模型
  • 将不同数据源接入到语言模型的交互中
  • 允许语言模型与运行环境交互
LangChain 中提供的模块
  • Modules:支持的模型类型和集成。
  • Prompt:提示词管理、优化和序列化。
  • Memory:内存是指在链/代理调用之间持续存在的状态。
  • Indexes:当语言模型与特定于应用程序的数据相结合时,会变得更加强大-此模块包含用于加载、查询和更新外部数据的接口和集成。
  • Chain:链是结构化的调用序列(对LLM或其他实用程序)
  • Agents: 代理是一个链,其中 LLM 在给定高级指令和一组工具的情况下,反复决定操作,执行操作并观察结果,直到高级指令完成。
  • Callbacks: 回调允许您纪录和流式传输任何链的中间步骤,从而轻松观察、调试和评估应用程序的内部。
LangChain 应用场景
  • 文档问答 : 在特定文档上回答问题,仅利用这些文档的信息来构建答案。
  • 个人助理 : LangChain 的主要用例之一。个人助理需要采取行动,记住交互,并了解您的数据。
  • 查询表格数据: 使用语言模型查询表类型结构化数据(CSV、SQL、DataFrame等)
  • 与API交互: 使用语言模型与API交互非常强大。它允许他们访问最新信息,并允许他们采取行动。
  • 信息提取:从文本中提取结构化信息。
  • 文档总结:压缩较长文档,一种数据增强生成。
用户输入

能够接入的数据类型。加工后的提问内容

PPT、图片、HTML、PDF等非结构文件并转换为文本信息。


自定义

python 复制代码
from openai import OpenAI
client = OpenAI(api_key="xxx")

assistant = client.beta.assistants.create(
    name="Math Tutor",
    instructions="You are a personal math tutor. Write and run code to answer math questions.",
    tools=[{"type": "code_interpreter"}],
    model="gpt-3.5-turbo-1106"
)

import json
import os


def show_json(obj):
    print(json.loads(obj.model_dump_json()))

show_json(assistant)

# create threads
thread = client.beta.threads.create()
show_json(thread)

# add content
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
)
show_json(message)

# run
run = client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant.id,
    instructions="Please address the user as Andy. The user has a premium account."
)
show_json(run)

import time

def wait_on_run(run, thread):
    while run.status == "queued" or run.status == "in_progress":
        run = client.beta.threads.runs.retrieve(
            thread_id=thread.id,
            run_id=run.id,
        )
        time.sleep(0.5)
    return run

run = wait_on_run(run, thread)
show_json(run)

messages = client.beta.threads.messages.list(thread_id=thread.id)
show_json(messages)

参考文档:

https://sms-activate.org

https://zhuanlan.zhihu.com/p/608652730 #pytorch model

https://blog.csdn.net/Darlight/article/details/136620892

相关推荐
shut up14 小时前
LangChain - 如何使用阿里云百炼平台的Qwen-plus模型构建一个桌面文件查询AI助手 - 超详细
人工智能·python·langchain·智能体
liliangcsdn15 小时前
如何基于ElasticsearchRetriever构建RAG系统
大数据·elasticsearch·langchain
东方佑15 小时前
基于FastAPI与LangChain的Excel智能数据分析API开发实践
langchain·excel·fastapi
猫头虎1 天前
Paper2Agent:将科研论文转化为可交互的AI智能体工具项目
人工智能·prompt·aigc·交互·pip·agi·ai-native
声网1 天前
阿里发布「夸克 AI 眼镜」:融合阿里购物、地图、支付生态;苹果拟收购计算机视觉初创 Prompt AI丨日报
人工智能·计算机视觉·prompt
大模型教程2 天前
搞懂 LangChain RAG:检索、召回原理及 docid 的关键意义
程序员·langchain·llm
Wade_Crab2 天前
第二章:动态 Prompt 管理与多科室智能问答系统
人工智能·spring·prompt
AI大模型2 天前
别再死磕 Chain 了!想做复杂的 Agent,你必须得上 LangGraph
程序员·langchain·llm
玲小珑2 天前
LangChain.js 完全开发手册(十四)生产环境部署与 DevOps 实践
前端·langchain·ai编程
weixin_438077493 天前
langchain官网翻译:Build a Question/Answering system over SQL data
数据库·sql·langchain·agent·langgraph