【星海出品】Langchain Prompt template

2024-06-01 03:20

本文主要是介绍【星海出品】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 ChatOpenAIllm = ChatOpenAI()

Method two

from langchain_openai import ChatOpenAIllm = 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 HumanMessagea = '1'
except:try:from langchain.schema import HumanMessagea = '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 StrOutputParseroutput_parser = StrOutputParser()chain = prompt | llm | output_parserchain.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 osdef 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 timedef 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 runrun = 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

这篇关于【星海出品】Langchain Prompt template的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1019849

相关文章

LangChain转换链:让数据处理更精准

1. 转换链的概念 在开发AI Agent(智能体)时,我们经常需要对输入数据进行预处理,这样可以更好地利用LLM。LangChain提供了一个强大的工具——转换链(TransformChain),它可以帮我们轻松实现这一任务。 转换链(TransformChain)主要是将 给定的数据 按照某个函数进行转换,再将 转换后的结果 输出给LLM。 所以转换链的核心是:根据业务逻辑编写合适的转换函

认识LangChain

介绍 LangChain 是一个用于开发由大型语言模型(LLM)支持的应用程序的框架。LangChain使得构建LLM应用更简单,大致三个阶段: 开发阶段 Conceptual guide | 🦜️🔗 LangChainProviders | 🦜️🔗 LangChainTemplates | 🦜️🔗 LangChain使用LangChain的开源构建块和组件构建您的应用程序。使

Transformers和Langchain中几个组件的区别

1.对于Transformers框架的介绍 1.1 介绍: transformers 是由 Hugging Face 开发的一个开源库,它提供了大量预训练模型,主要用于自然语言处理(NLP)任务。这个库提供的模型可以用于文本分类、信息抽取、问答、文本生成等多种任务。 1.2 应用场景: 文本分类:使用 BERT、RoBERTa 等模型进行情感分析、意图识别等。命名实体识别(NER):使用序列

基于LangChain框架搭建知识库

基于LangChain框架搭建知识库 说明流程1.数据加载2.数据清洗3.数据切分4.获取向量5.向量库保存到本地6.向量搜索7.汇总调用 说明 本文使用openai提供的embedding模型作为框架基础模型,知识库的搭建目的就是为了让大模型减少幻觉出现,实现起来也很简单,假如你要做一个大模型的客服问答系统,那么就把历史客服问答数据整理好,先做数据处理,在做数据向量化,最后保

AI 大模型企业应用实战(11)-langchain 的Document Loader机制

loader机制让大模型具备实时学习的能力: 0 Loader机制 案例环境准备: import osos.environ["OPENAI_API_KEY"] = "sk-javaedge"os.environ["OPENAI_PROXY"] = "https://api.chatanywhere.tech"import osfrom dotenv import load_doten

AI大模型企业应用实战(14)-langchain的Embedding

1 安装依赖 ! pip install --upgrade langchain! pip install --upgrade openai==0.27.8! pip install -U langchain-openai ! pip show openai! pip show langchain! pip show langchain-openai 2 Embed_document

Prompt 写作提示经验:完整格式和技巧

编写prompt以确保输出格式通常需要明确指定您期望的输出结构和内容要求。以下是一些确保输出格式的步骤和技巧: 明确指定格式:在prompt中明确指出您期望的输出格式。例如,如果您需要一个包含标题、子标题和段落的文章,应在prompt中指明。 使用模板:提供一个或多个模板作为参考。模板可以是文本格式的描述,也可以是实际的示例。 包含示例:给出一个或多个正确格式的示例输出,这样模型可以学习并

AI 大模型企业应用实战(09)-LangChain的示例选择器

1 根据长度动态选择提示词示例组 1.1 案例 根据输入的提示词长度综合计算最终长度,智能截取或者添加提示词的示例。 from langchain.prompts import PromptTemplatefrom langchain.prompts import FewShotPromptTemplatefrom langchain.prompts.example_selector

ChatGPT-4o也参加高考了,还写了六大考卷的全部作文! |【WeThinkIn出品】

写在前面 【WeThinkIn出品】栏目专注于分享Rocky的最新思考与经验总结,包含但不限于技术领域。欢迎大家一起交流学习💪 欢迎大家关注Rocky的公众号:WeThinkIn 欢迎大家关注Rocky的知乎:Rocky Ding AIGC算法工程师面试面经秘籍分享:WeThinkIn/Interview-for-Algorithm-Engineer欢迎大家Star~ 获取更多AI行

How to create a langchain doc from an str

问题背景: I've searched all over langchain documentation on their official website but I didn't find how to create a langchain doc from a str variable in python so I searched in their GitHub code and I