LangChain--如何使用大模型
LangChain是一个用于构建和管理语言模型链的开源框架,旨在帮助开发者更高效地构建和部署复杂的自然语言处理(NLP)应用。LangChain自身并不开发LLMs,它的核心理念是为各种LLMs实现通用的接口,把LLMs相关的组件“链接”在一起,简化LLMs应用的开发难度。
大家好,我是小森( ﹡ˆoˆ﹡ ) ! 易编橙·终身成长社群创始团队嘉宾,橙似锦计划领衔成员、阿里云专家博主、腾讯云内容共创官、CSDN人工智能领域优质创作者 。
LangChain是一个用于构建和管理语言模型链的开源框架,旨在帮助开发者更高效地构建和部署复杂的自然语言处理(NLP)应用。LangChain自身并不开发LLMs,它的核心理念是为各种LLMs实现通用的接口,把LLMs相关的组件“链接”在一起,简化LLMs应用的开发难度
LangChain的应用场景非常广泛:智能问答系统、文本生成、信息提取、智能客服等等。
LangChain主要组件
- Models:模型
- Prompts:提示
- Memory:记忆
- Indexes:索引
- Chains:链,一系列对各种组件的调用
- Agents:代理,决定模型采取哪些行动
Models
Models模块提供了与不同类型语言模型进行交互的接口和能力。这些语言模型包括但不限于OpenAI的GPT系列、Google的LaMDA、Meta的LLaMa等。通过Models模块,开发者可以轻松地接入和使用这些强大的语言模型,构建复杂的自然语言处理任务。
在LangChain中,语言模型被分为LLMs、Chat Models和Text Embedding三类。Models模块针对不同类型的模型提供了相应的类和操作方法,以满足不同的应用场景需求。
常用大模型的下载库:huggingface、modelscope
pip install langchain
pip install qianfan
我们可以去百度千帆大模型平台,创建模型,调用API Key 和 Secret Key:
我们可以通过langchain调用文心一言4.0模型:
import os
from langchain_community.llms import QianfanLLMEndpoint
os.environ['QIANFAN_AK'] = "*********"
os.environ['QIANFAN_SK'] = "*********"
llm = QianfanLLMEndpoint(model="ERNIE-Bot-turbo")
res = llm("请写一首诗把")
print(res)
输出:
好的,以下是一首关于春天的诗:
春风轻拂绿意浓,
花开满园醉人心。
蝴蝶翩翩飞舞处,
小鸟欢歌乐不停。
田野间麦浪滚滚,
桃花笑语映日红。
万物复苏生机起,
春日美景入诗中。
Chat Models
Chat Models模块专注于处理和交互基于聊天消息格式的语言模型。与传统的LLMs(大型语言模型)不同,Chat Models的输入和输出是格式化的聊天消息,这使得它们更适合处理对话式任务和生成更加自然、上下文相关的响应。
import os
from langchain_community.chat_models import QianfanChatEndpoint
from langchain.schema.messages import HumanMessage
os.environ['QIANFAN_AK'] = "***"
os.environ['QIANFAN_SK'] = "***"
chat = QianfanChatEndpoint(model="ERNIE-Bot-turbo")
messages = [HumanMessage(content="给我写一首唐诗")]
res = chat(messages)
print(res)
输出:
content='当然可以。这是一首名为《秋日思乡》的唐诗:\n\n秋风吹过菊花黄,\n归心似箭逐月行。\n故土犹在梦难留,\n望断天涯泪满衣。' additional_kwargs={'finish_reason': '', 'request_id': 'as-508nd0m8r2', 'object': 'chat.completion', 'search_info': [], 'usage': {'prompt_tokens': 5, 'completion_tokens': 50, 'total_tokens': 55}} response_metadata={'token_usage': {'prompt_tokens': 5, 'completion_tokens': 50, 'total_tokens': 55}, 'model_name': 'ERNIE-Bot-turbo', 'finish_reason': 'stop', 'id': 'as-508nd0m8r2', 'object': 'chat.completion', 'created': 1722134892, 'result': '当然可以。这是一首名为《秋日思乡》的唐诗:\n\n秋风吹过菊花黄,\n归心似箭逐月行。\n故土犹在梦难留,\n望断天涯泪满衣。', 'is_truncated': False, 'need_clear_history': False, 'usage': {'prompt_tokens': 5, 'completion_tokens': 50, 'total_tokens': 55}} id='run-871f99c6-248e-48d9-91c6-7e7779bb59b2-0' usage_metadata={'input_tokens': 5, 'output_tokens': 50, 'total_tokens': 55}
提示模板
提示模板就是把一些常见的提示整理成模板,用户只需要修改模板中特定的词语,就能快速准确地告诉模型自己的需求。
import os
from langchain_community.chat_models import QianfanChatEndpoint
from langchain.prompts import ChatPromptTemplate
os.environ['QIANFAN_AK'] = "***"
os.environ['QIANFAN_SK'] = "***"
template_str = """你是一位专业的小红书运营官。\n
对于售价为 {price} 元的 {flower_name} ,您能提供一个吸引人的简短描述吗? 一句话"""
promp_emplate = ChatPromptTemplate.from_template(template_str)
prompt = promp_emplate.format_messages(flower_name=["玫瑰"], price='50')
print('prompt-->', prompt)
chat = QianfanChatEndpoint(
streaming=True,model="ERNIE-Bot-turbo"
)
result = chat(prompt)
print(result)
输出:
content='珍贵的玫瑰花瓣,如同初升的朝阳般温暖而甜美,为您的生活增添一丝独特的香气和温暖。🌹 💕 (仅供参考,请根据实际情况调整话语)' response_metadata={'token_usage': {'prompt_tokens': 35, 'completion_tokens': 37, 'total_tokens': 72}, 'model_name': 'ERNIE-Bot-turbo', 'finish_reason': 'stop'} id='run-c15bff74-741d-4004-9412-ad6003e8f96c-0'
Embeddings Models
Embeddings Models可以为文本创建向量映射,这样就能在向量空间里去考虑文本;在NLP中,Embedding的作用就是将数据进行文本向量化。
import os
from langchain_community.embeddings import QianfanEmbeddingsEndpoint
os.environ['QIANFAN_AK'] = "***"
os.environ['QIANFAN_SK'] = "***"
embed = QianfanEmbeddingsEndpoint()
res1 = embed.embed_query('我爱你')
print(res1)
res2 = embed.embed_documents(['我爱你', '我喜欢你'])
print(res2)
输出:
[INFO][2024-07-28 10:56:16.725] oauth.py:228 [t:5544]: trying to refresh access_token for ak `CNzMCb***`
[INFO][2024-07-28 10:56:16.988] oauth.py:243 [t:5544]: sucessfully refresh access_token
[0.17311938107013702, -0.0963786169886589, 0.07811204344034195, 0.039995335042476654, 0.005461528431624174, -0.045833807438611984, -0.026092199608683586, -0.019432438537478447, -0.017971372231841087, 0.056535426527261734, 0.048402175307273865, -0.054045066237449646, -0.04589874669909477, 0.05654500052332878, -0.07421743124723434, -0.024018535390496254, -0.09536591917276382, 0.020946761593222618, -0.01938055269420147, -0.0009667843696661294, 0.12385024130344391, -0.08784071356058121, 0.056188035756349564, -0.06564845889806747, 0.019532460719347, -0.006963981315493584, 0.025636369362473488, 0.009537937119603157, -0.04155377671122551, 0.07116766273975372, 0.0938122421503067, 0.021260013803839684, 0.03518209606409073, 0.008317320607602596, -0.0019243070855736732, 0.11220177263021469, 0.044228050857782364, 0.0460934080183506, -0.14091721177101135, 0.019789453595876694, 0.013656237162649632, -0.061166953295469284, -0.038639772683382034, -0.062454089522361755, 0.016178900375962257, -0.03011832945048809, -0.08439959585666656, 0.0022036435548216105, -0.07330037653446198, -0.10189015418291092, -0.05381982773542404, 0.04427957907319069, 0.01491308119148016, 0.03808053955435753, 0.02410702407360077, 0.03526068106293678, -0.05644663795828819, -0.030593788251280785, 0.10528215020895004, 0.0047299060970544815, -0.01718810573220253, -0.006194483954459429, -0.12342105805873871, -0.0019607245922088623, 0.03217269107699394, -0.010351389646530151, 0.0417243130505085, 0.029990680515766144, 0.09183978289365768, -0.04874948039650917, -0.04338744655251503, 0.02469373308122158, -0.019464919343590736, 0.10235122591257095, 3.215195101802237e-05, 0.036971136927604675, -0.0842595025897026, -0.05434097722172737, -0.002266199328005314, 0.018721122294664383, 0.046459950506687164, -0.011437651701271534, 0.005042331293225288, 0.004760058596730232, -0.019767258316278458, -0.020541315898299217, -0.04401717334985733, 0.03678600862622261, 0.011489552445709705, -0.0820135772228241, 0.03179977461695671, -0.024241261184215546, 0.07592024654150009, -0.03596271574497223, -0.12544889748096466, -0.05337859317660332, 0.011707315221428871, -0.08666867762804031, 0.028518740087747574, 0.06716343015432358, -0.052857350558042526, -0.06656564027070999, 0.0430540069937706, 0.03919413313269615, -0.000551051867660135, 0.06644643843173981, 0.02871069312095642, -0.13079498708248138, 0.07267789542675018, 0.03879015892744064, -0.0626017153263092, 0.06336624175310135, 0.01577211171388626, 0.0015874304808676243, -0.09587012976408005, 0.08869410306215286, -0.12326595187187195, -0.048301536589860916, -0.04627477750182152, -0.043050531297922134, 0.0045365625992417336, -0.011143677867949009, 0.17218466103076935, 0.10462985187768936, -0.05481578782200813, 0.0205503199249506, -0.031914737075567245, 0.04180685803294182, -0.08946213126182556, 0.0643886998295784, -0.045430514961481094, -0.03911157697439194, 0.04744989424943924, -0.055206093937158585, -0.04333275556564331, -0.016984358429908752, -0.03152317926287651, 0.001480796025134623, -0.06254784017801285, 0.01827562041580677, -0.07848525047302246, 0.0761973038315773, 0.0003041250165551901, -0.018855556845664978, 0.09780651330947876, 0.10803353786468506, 0.0842244103550911, -0.027804864570498466, -0.08139058947563171, 0.04273829609155655, -0.018516382202506065, 0.08864732086658478, -0.1488604098558426, -0.029488295316696167, 0.011920991353690624, -0.04462737962603569, 0.0056211198680102825, 0.0067628188990056515, 0.012444596737623215, -0.052288733422756195, -0.06419272720813751, -0.032742660492658615, -0.024124518036842346, 0.01012441050261259, 0.03110273741185665, -0.03909829631447792, -0.04320163652300835, 0.013529632240533829, -0.022782107815146446, 0.04383174329996109, -0.012891656719148159, 0.15987786650657654, -0.015665791928768158, -0.054463084787130356, -0.09946627914905548, -0.03700590878725052, -0.0010551792802289128, 0.1662973016500473, -0.07429419457912445, -0.009254837408661842, -0.012464023195207119, 0.012699784711003304, 0.0502377524971962, -0.004791815299540758, 0.041303977370262146, -0.052285585552453995, -0.04075608775019646, 0.010103428736329079, 0.06692855805158615, -0.06185309216380119, -0.0644482672214508, 0.021635638549923897, 0.10284976661205292, 0.0012724905973300338, 0.13313627243041992, -0.03541024029254913, 0.008838660083711147, -0.02354736439883709, -0.002149595646187663, -0.13542407751083374, -0.0074654086492955685, 0.11356885731220245, -0.0012978651793673635, -0.08244557678699493, 0.04902956634759903, -0.0063301557675004005, 0.05374397337436676, 0.01589476503431797, -0.024317225441336632, 0.10789807140827179, -0.026865793392062187, 0.04794687405228615, 0.005194827448576689, -0.12212704122066498, -0.019639208912849426, 0.09773065149784088, 0.08052042871713638, 0.06434154510498047, 0.1341923326253891, -0.03745073825120926, -0.027674179524183273, 0.0643153265118599, 0.07157416641712189, 0.053725406527519226, -0.05453205481171608, 0.0767812579870224, 0.10964184254407883, 0.04158182069659233, -0.004959672689437866, -0.12060683965682983, 0.022289268672466278, 0.014868199825286865, -0.032756395637989044, 0.05342009291052818, 0.016302553936839104, 0.03813028708100319, 0.046586524695158005, -0.004367701709270477, -0.02919246256351471, 0.0231705941259861, 0.012244513258337975, 0.02936718240380287, -0.03598557040095329, -0.0990867018699646, -0.10058268904685974, -0.006947755813598633, 0.09843306988477707, -0.05054671689867973, -0.032868918031454086, 0.02381402812898159, -0.06296893209218979, -0.08558419346809387, -0.02711925283074379, 0.007219271268695593, -0.008877587504684925, 0.014889375306665897, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.11387636512517929, 0, 0, 0, 0, -0.17244486510753632, 0, 0, 0, 0, 0, 0, 0, 0, -0.12753252685070038, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[[0.1731303632259369, -0.09634850174188614, 0.07816127687692642, 0.039980221539735794, 0.0054778545163571835, -0.045868244022130966, -0.026062440127134323, -0.019449805840849876, -0.01801307499408722, 0.05654280632734299, 0.04836028814315796, -0.05404885485768318, -0.04593881592154503, 0.05657430738210678, -0.07418347150087357, -0.024016546085476875, -0.0953541100025177, 0.020964322611689568, -0.01933935470879078, -0.0010001560440286994, 0.12386968731880188, -0.08785390108823776, 0.05621049925684929, -0.06561771035194397, 0.01953473687171936, -0.006997905671596527, 0.025619538500905037, 0.009538337588310242, -0.041529443114995956, 0.07118349522352219, 0.09377264231443405, 0.021254587918519974, 0.035202059894800186, 0.008299731649458408, -0.0019056544406339526, 0.11218218505382538, 0.044251520186662674, 0.04610373452305794, -0.14087629318237305, 0.01975364424288273, 0.013616025447845459, -0.061147887259721756, -0.03864365443587303, -0.06251809746026993, 0.016233500093221664, -0.030100420117378235, -0.08442800492048264, 0.0022232206538319588, -0.07335403561592102, -0.10186680406332016, -0.053831323981285095, 0.044278278946876526, 0.014872458763420582, 0.03808218240737915, 0.024127809330821037, 0.03528764471411705, -0.05642213672399521, -0.030611077323555946, 0.1052338257431984, 0.004743505734950304, -0.01719396747648716, -0.006177573464810848, -0.1234244704246521, -0.0020027922000736, 0.03217214345932007, -0.010359552688896656, 0.0416712649166584, 0.02997693419456482, 0.09184076637029648, -0.048722803592681885, -0.04337553679943085, 0.024697182700037956, -0.019479159265756607, 0.10236021876335144, -1.5271956499418593e-06, 0.036961499601602554, -0.08426558971405029, -0.054339464753866196, -0.0022730871569365263, 0.018683703616261482, 0.04648237302899361, -0.011451843194663525, 0.0050391703844070435, 0.004738252609968185, -0.019782893359661102, -0.020539889112114906, -0.04402852803468704, 0.03676902875304222, 0.011474682949483395, -0.08197476714849472, 0.03176981210708618, -0.024230262264609337, 0.0759047195315361, -0.03592647612094879, -0.12546169757843018, -0.05340857803821564, 0.011721273884177208, -0.08665436506271362, 0.028534121811389923, 0.06716544181108475, -0.052856698632240295, -0.06654420495033264, 0.04304922744631767, 0.039207734167575836, -0.0004969750298187137, 0.06642762571573257, 0.02873707190155983, -0.130813866853714, 0.07261481881141663, 0.03878645971417427, -0.06264033913612366, 0.06336422264575958, 0.015796495601534843, 0.0015605518128722906, -0.09584233909845352, 0.08869029581546783, -0.12321485579013824, -0.04828774556517601, -0.046266376972198486, -0.04305986315011978, 0.004502714611589909, -0.011150230653584003, 0.17217248678207397, 0.10460729151964188, -0.0548018254339695, 0.020583977922797203, -0.03194836527109146, 0.041807230561971664, -0.08947951346635818, 0.06438485532999039, -0.04543781280517578, -0.03910034894943237, 0.04743644967675209, -0.055203184485435486, -0.04334700480103493, -0.016934368759393692, -0.031540725380182266, 0.001511279260739684, -0.06253688037395477, 0.018283924087882042, -0.07847001403570175, 0.07623295485973358, 0.00027031931676901877, -0.018840251490473747, 0.09783163666725159, 0.1080268993973732, 0.084246926009655, -0.02776608057320118, -0.08138692378997803, 0.04269621893763542, -0.018515726551413536, 0.08865175396203995, -0.14890950918197632, -0.029510991647839546, 0.011920819990336895, -0.04463885724544525, 0.005628664046525955, 0.006768232211470604, 0.012483957223594189, -0.05229240655899048, -0.06421525776386261, -0.03276282921433449, -0.024104837328195572, 0.010178714990615845, 0.031090423464775085, -0.03905665501952171, -0.043247003108263016, 0.013554130680859089, -0.022804733365774155, 0.04385185241699219, -0.012912891805171967, 0.1598769873380661, -0.0156499445438385, -0.054463885724544525, -0.099462129175663, -0.03701178357005119, -0.0010660071857273579, 0.16629938781261444, -0.07428492605686188, -0.009230648167431355, -0.012454706244170666, 0.012658101506531239, 0.05026748403906822, -0.004758336581289768, 0.04129819944500923, -0.052276019006967545, -0.04077630862593651, 0.010061093606054783, 0.0669349655508995, -0.06186607852578163, -0.06442253291606903, 0.021678004413843155, 0.10286121815443039, 0.0012748338049277663, 0.13312067091464996, -0.03543088957667351, 0.008818977512419224, -0.02351522073149681, -0.0021335084456950426, -0.13540764153003693, -0.0075097777880728245, 0.11360612511634827, -0.0013167397119104862, -0.08245868980884552, 0.048978742212057114, -0.006350378040224314, 0.053741455078125, 0.015910958871245384, -0.024317460134625435, 0.1078944057226181, -0.026878416538238525, 0.0479239821434021, 0.00523604778572917, -0.12213002890348434, -0.019617265090346336, 0.09775438904762268, 0.080501988530159, 0.06435265392065048, 0.13420188426971436, -0.03745100274682045, -0.027692310512065887, 0.06433158367872238, 0.07156147062778473, 0.0536842942237854, -0.05452205613255501, 0.07680387794971466, 0.1096375361084938, 0.04158282279968262, -0.004957149736583233, -0.12058490514755249, 0.022296424955129623, 0.014845090918242931, -0.03276931867003441, 0.053395140916109085, 0.016309699043631554, 0.03810003399848938, 0.0465787835419178, -0.004363955929875374, -0.02916048839688301, 0.023168154060840607, 0.012247135862708092, 0.029369663447141647, -0.03598378971219063, -0.0990738645195961, -0.10057568550109863, -0.00692224083468318, 0.09848365932703018, -0.05058170482516289, -0.03288383409380913, 0.023826459422707558, -0.06296761333942413, -0.08555896580219269, -0.027183089405298233, 0.007223169319331646, -0.008823983371257782, 0.014906640164554119, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.11388964205980301, 0, 0, 0, 0, -0.17246344685554504, 0, 0, 0, 0, 0, 0, 0, 0, -0.1275666505098343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0.16389910876750946, -0.06770157068967819, 0.04045385494828224, 0.12051095813512802, -0.001777258119545877, -0.062474220991134644, -0.05002710968255997, -0.014913847669959068, 0.017864150926470757, 0.02574882097542286, 0.022807372733950615, -0.041328828781843185, -0.02558823674917221, 0.04861540347337723, -0.029599899426102638, 0.013830029405653477, -0.1407146006822586, -0.002121110912412405, 0.008856385946273804, -0.005490437615662813, 0.06555238366127014, -0.10115061700344086, 0.045327723026275635, -0.07834116369485855, 0.01891659013926983, -0.014293101616203785, 0.013221134431660175, -0.004415486473590136, -0.005760273430496454, 0.08298999071121216, 0.14221566915512085, 0.01962032914161682, 0.04692693427205086, -0.016003498807549477, -0.046685487031936646, 0.0973377600312233, 0.03330681100487709, 0.0039037258829921484, -0.1496174931526184, 0.004967049695551395, 0.03225664794445038, -0.10554232448339462, -0.008176950737833977, -0.013541444204747677, 0.02877664379775524, -0.04292839765548706, -0.061834465712308884, 0.007461403030902147, -0.0824197456240654, -0.027035556733608246, -0.022163042798638344, 0.07449305802583694, 0.005943989381194115, 0.03000137396156788, 0.012065282091498375, 0.02709725871682167, 0.028293050825595856, -0.07819213718175888, 0.0936780720949173, -0.02314838580787182, -0.05561741068959236, 0.013178414665162563, -0.08483412861824036, 0.0013117095222696662, 0.08647595345973969, 0.030305908992886543, 0.03919273614883423, 0.06268090009689331, 0.09513470530509949, -0.028437228873372078, 0.01003415696322918, 0.04277034476399422, -0.02100389637053013, 0.0657079815864563, 0.035147592425346375, 0.0313875675201416, -0.08001073449850082, -0.060808081179857254, -0.00380363198928535, 0.008916310966014862, 0.05403676629066467, -0.0660042017698288, -0.020998509600758553, -0.03541043773293495, -0.019833741709589958, -0.05287456512451172, -0.04867412522435188, 0.03675905987620354, 0.023823507130146027, -0.0426037572324276, -0.01942325197160244, -0.05950932204723358, 0.02904636412858963, 0.00010870093683479354, -0.12397242337465286, -0.028665585443377495, -0.009277568198740482, -0.1302555948495865, 0.03387037664651871, 0.1270952671766281, -0.0642475113272667, -0.040525030344724655, 0.05341024696826935, 0.033968258649110794, 0.01654127798974514, 0.08043668419122696, -0.006379600148648024, -0.12655603885650635, 0.09320463985204697, 0.0823647528886795, -0.09285281598567963, 0.04959321394562721, -0.009988158009946346, -0.013809377327561378, -0.12832705676555634, 0.08220911771059036, -0.052709806710481644, 0.023893173784017563, -0.036447469145059586, -0.02997385896742344, 0.00121726980432868, -0.0189362820237875, 0.06822358816862106, 0.15930724143981934, -0.025683922693133354, 0.05501779541373253, -0.019372737035155296, -0.007177039980888367, -0.055258847773075104, 0.06032353639602661, 0.02113361842930317, 0.0033874008804559708, 0.060398783534765244, -0.07765013724565506, -0.019809719175100327, -0.038755737245082855, -0.049449872225522995, 0.00031932935235090554, -0.09415747970342636, 0.050694216042757034, -0.04579533636569977, 0.007168720010668039, 0.0020155622623860836, -0.012319398112595081, 0.1080072745680809, 0.06736469268798828, 0.06378096342086792, -0.01779053546488285, -0.05347374826669693, 0.013121265918016434, -0.04967748373746872, 0.10891570150852203, -0.10875508934259415, -0.031661078333854675, -0.0053736744448542595, -0.06597929447889328, 0.009702722541987896, -0.02499106526374817, 0.03875891491770744, -0.016639281064271927, -0.0549413338303566, -0.05765439197421074, 0.012186153791844845, -0.013905646279454231, 0.01614615134894848, -0.07023987174034119, 0.027266694232821465, 0.08325576782226562, -0.05654871091246605, 0.03753071278333664, -0.04701317474246025, 0.15308474004268646, -0.025858959183096886, -0.011655387468636036, -0.10311544686555862, 0.03137284144759178, -0.014122921042144299, 0.128634974360466, -0.02543693222105503, -0.009841613471508026, -0.04653692990541458, 0.004685093183070421, -0.004517170134931803, -0.03267235308885574, 0.03400425612926483, -0.08803475648164749, -0.07121620327234268, -0.009479966945946217, 0.03879011794924736, -0.028311043977737427, -0.05191628634929657, 0.03414444252848625, 0.08767206221818924, 0.022109854966402054, 0.10569605976343155, -0.023431982845067978, -0.012663538567721844, -0.04202290624380112, -0.005510961636900902, -0.07364701479673386, -0.06781359016895294, 0.1226738914847374, -0.0443853922188282, -0.03154117241501808, 0.01912350207567215, 0.0038300000596791506, 0.031059106811881065, 0.00041550720925442874, -0.00885219406336546, 0.045320067554712296, -0.02487872913479805, 0.06142089143395424, -0.03697742149233818, -0.13536281883716583, 0.020091462880373, 0.08493854850530624, 0.07990704476833344, 0.06457094848155975, 0.12465929239988327, -0.004836426116526127, -0.06268123537302017, 0.07718651741743088, 0.10354164987802505, 0.03288743272423744, -0.05115475133061409, 0.0035111596807837486, 0.15599209070205688, 0.028728671371936798, -0.010757789947092533, -0.1506638526916504, 0.01406779419630766, 0.04831463843584061, -0.04138438403606415, 0.010591553524136543, 0.02598242089152336, -0.05162525922060013, 0.045510221272706985, -0.011666939593851566, -0.052717097103595734, 0.041030675172805786, 0.003010846208781004, 0.07883022725582123, -0.05143994837999344, -0.09701495617628098, -0.032211508601903915, -0.06290502101182938, 0.10223261266946793, -0.027773438021540642, -0.03940271958708763, 0.05758827552199364, -0.033425021916627884, -0.0805060863494873, -0.07255500555038452, 0.05357032269239426, 0.00097850919701159, 0.03717030584812164, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.08706239610910416, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.12385813146829605, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.23807896673679352, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Prompts
Prompt是指当用户输入信息给模型时加入的提示,这个提示的形式可以是zero-shot或者few-shot等方式,目的是让模型理解更为复杂的业务场景以便更好的解决问题。
import os
from langchain import PromptTemplate
from langchain_community.llms import QianfanLLMEndpoint
os.environ['QIANFAN_AK'] = "***"
os.environ['QIANFAN_SK'] = "***"
template = "我的邻居姓{lastname},他生了个儿子,给他儿子起个名字"
prompt = PromptTemplate(
input_variables=["lastname"],
template=template,
)
prompt_text = prompt.format(lastname="贾")
print(prompt_text)
llm = QianfanLLMEndpoint()
result = llm(prompt_text)
print(result)
输出:
我的邻居姓贾,他生了个儿子,给他儿子起个名字
D:\apollo\anaconda\lib\site-packages\langchain_core\_api\deprecation.py:139: LangChainDeprecationWarning: The method `BaseLLM.__call__` was deprecated in langchain-core 0.1.7 and will be removed in 0.3.0. Use invoke instead.
warn_deprecated(
[INFO][2024-07-28 11:03:13.756] oauth.py:228 [t:29636]: trying to refresh access_token for ak `CNzMCb***`
[INFO][2024-07-28 11:03:14.095] oauth.py:243 [t:29636]: sucessfully refresh access_token
给邻居家的新生儿起名字是一件非常重要的事情,需要考虑到很多因素,如家庭传统、文化背景、父母的偏好等等。基于您提供的信息,我为您提供以下几个名字供您参考:
1. 贾瑞安:这个名字寓意着平安、吉祥,是一个非常美好的名字。
2. 贾俊豪:这个名字寓意着英俊、豪爽,是一个很有男子气概的名字。
3. 贾宇航:这个名字寓意着广阔的天地、无限的可能,适合有远大抱负的孩子。
4. 贾浩然:这个名字寓意着浩渺、自然,是一个很自然的名字。
5. 贾睿诚:这个名字寓意着睿智、诚实,是一个很有品行的名字。
6. 贾梓轩:这个名字适合有艺术天赋或喜欢音乐的孩子,寓意着广阔的舞台和无限的可能。
- zero-shot学习通常通过精心设计的提示(prompt)来实现。可以构建一个包含适当上下文和指令的提示,然后将其传递给LLM,引导模型在没有任何特定任务示例的情况下完成任务。
- few-shot场景中为LLM提供少量的任务示例作为上下文。这些示例展示了如何执行任务,并帮助模型快速理解任务的要求。
- Few-shot学习在LangChain中特别有用,因为它可以在少量样本的情况下提高模型的性能。
few-shot来使用提示
from langchain_community.llms import QianfanLLMEndpoint
import os
from langchain import PromptTemplate, FewShotPromptTemplate
os.environ['QIANFAN_AK'] = "***"
os.environ['QIANFAN_SK'] = "***"
examples = [
{"word": "开心", "fanyici": "难过"},
{"word": "黑", "fanyici": "白"},
]
example_template = """
单词: {word}
反义词: {fanyici}\\n
"""
example_prompt = PromptTemplate(
input_variables=["word", "fanyici"],
template=example_template,
)
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix="给出每个单词的反义词",
suffix="单词: {input}\\n反义词:",
input_variables=["input"],
example_separator="\\n",
)
prompt_text = few_shot_prompt.format(input="亮")
print(prompt_text)
print('*'*30)
llm = QianfanLLMEndpoint(model='Qianfan-Chinese-Llama-2-7B')
print(llm(prompt_text))
输出:
给出每个单词的反义词\n
单词: 开心
反义词: 难过\n
\n
单词: 黑
反义词: 白\n
\n单词: 亮\n反义词:
******************************
暗
我们可以通过打印出来的提示词观察到,prefix参数就是前缀的提示词,examples是给出的实例,example_prompt定义了如何将每个示例格式化为字符串(规定了实例怎么被输出出来)。
Chains(链)
Chains可以简单理解为对组件的调用序列,其中可以包括其他Chains。在LangChain中,Chains可以是非常简单的,如只包含一个Prompt模板和大型语言模型(LLM)的LLMChain;也可以是更复杂的,涉及多个步骤和多种组件的调用。Chains的主要特点是其输出会成为下一个组件或Chain的输入,从而实现功能的串联。
Chains的类型
LangChain中Chains的类型多样,以满足不同的需求。以下是一些常见的Chains类型:
-
LLMChain:最基本的Chains类型,整合了大型语言模型和提示模板。它接受用户输入,通过提示模板格式化后传递给LLM,并返回LLM的响应。
-
TransformChain:用于处理Chains之间的输入和输出,支持自定义的转换函数,便于Chains之间的数据传输。
-
SequentialChain:顺序链,允许将多个Chains按顺序组合起来,每个Chain的输出成为下一个Chain的输入。这可以用于执行多步骤的任务。
-
SimpleSequentialChain:与SequentialChain类似,但每个步骤都有一个单一的输入/输出。
-
异步Chains:用于执行异步函数和任务,如异步LLMChain等。
-
实用工具Chains:包含各种实用工具的Chains,如SQL数据库Chains、Bash Chains等,允许将自然语言转换为SQL查询、运行Bash命令等。
from langchain_community.llms import QianfanLLMEndpoint
import os
from langchain import PromptTemplate, FewShotPromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SimpleSequentialChain
os.environ['QIANFAN_AK'] = "***"
os.environ['QIANFAN_SK'] = "***"
template = "我的邻居姓{lastname},他生了个儿子,给他儿子起个名字"
first_prompt = PromptTemplate(
input_variables=["lastname"],
template=template,
)
llm = QianfanLLMEndpoint()
first_chain = LLMChain(llm=llm, prompt=first_prompt)
# 创建第二条链
second_prompt = PromptTemplate(
input_variables=["child_name"],
template="邻居的儿子名字叫{child_name},给他起一个小名",
)
second_chain = LLMChain(llm=llm, prompt=second_prompt)
# 链接两条链
overall_chain = SimpleSequentialChain(chains=[first_chain, second_chain], verbose=True)
print(overall_chain)
catchphrase = overall_chain.run("孙")
print(catchphrase)
输出:
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)