【星海出品】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

pip install langchain-openai
STEP 2

Method one
OpenAI-api

export OPENAI_API_KEY="your-api-key"

from langchain_openai import ChatOpenAI

llm = ChatOpenAI()

Method two

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(api_key="...")
STEP 3
llm.invoke("how can langsmith help with testing?")

one or two also support predict

predict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any)str
llm.predict("hello!")
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)
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.

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?"})

还可以添加输出解析器

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等非结构文件并转换为文本信息。


自定义


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值