>头
在这个故事中,我们将深入了解 LangChain’s LLM链 class. 根据 LangChain的文档 LLM链 允许定义提示模板,然后将键值对列表发送到提示模板,以便大型语言模型进行处理.
你通常会使用 LLM链 使用像这样的工作流:
步骤如下:
Key_value_list = [ {'内容':“整个英国, 化石燃料公司没有兑现承诺,留下了伤痕累累、污染严重的景观, 没有人被追究责任。”, 标题:“威尔士露天采矿的有毒遗产”}, {'内容': "解决方案不是更多字段,而是更好, 更紧凑, 零残忍和无污染的工厂”, 'title': '我们认为农业好,工厂坏' '},]
以便更好地了解您可以如何使用 LLM链我们创建了一个简单的脚本,它将多个提示模板应用于 TheGuardian的 (一份英国报纸)RSS订阅. RSS提要包含基于文本的记录列表,这些记录可以转换为键值列表.
这个脚本的目的是获得情感, 一组文章的关键词,并根据预定义的一组类别对文章进行分类.
然后,脚本计算在专栏作家的RSS提要中找到的情绪、类别和关键词:
我们使用的模型是gpt-3.5-turbo”
脚本可以在Github上查看:
http://github.com/gilfernandes/llm_chain_out/blob/main/langchain_llm_chain_extract.py
命令行脚本执行如下操作:
对于sys中的url.argv [1]: process_url (url)def process_url (url): """ 提取每个RSS提要的内容. 将每个RSS提要的内容发送到LLMChain,以便将提示应用于提取的记录. 为每个RSS提要创建一个数据集,该数据集结合LLM的输出并从中生成HTML和Excel文件. :param url: RSS提要的url,如e.g: http://www.theguardian.com/profile/georgemonbiot/rss """ 打印(f”处理{url}”) Zipped_results = [] Llm_responses = [] Input_list = extract_rss(url) 对于prompt_templates中的prompt_template: llm_responses.追加(process_llm (input_list, prompt_template)) sentiment_counter = Counter() categores_counter = Counter() 对于zip(input_list, *llm_responses)中的压缩: 情绪={'情绪':压缩[1]['text']} Categorized_sentiment = categorize_sentiment(已压缩[1]['text']) Sentiment_counter [categorized_sentiment] += 1 Sentiment_category = {' Sentiment_category ': categorized_sentiment} Keywords = {' Keywords ': zipped[2]['text']} Raw_categories = zipped[3]['text'] 分类= {' Classification ': raw_categories} Sanitized_topics = sanitize_categories(raw_categories) categories_counter.更新(消毒_topics) Sanitized_categories = {'topics': ",".十大正规博彩网站评级(消毒_topics)} Full_record = { * *压缩[0], * *情绪, * *关键字, * * sentiment_category, * *分类, * * 消毒_categories } zipped_results.追加(full_record) Result_df = pd.DataFrame (zipped_results) Title = url.取代 (":", "_").取代 ("/", "_") Serialize_results (url, result_df, title, sentiment_counter, categores_counter)
def extract_rss (url): """ 从URL中提取内容和标题. :param url RSS提要的url,如e.g: http://www.theguardian.com/pro文件/georgemonbiot/rs """ 响应=请求.得到(url) tree = ElementTree.fromstring(响应.内容) 内容= [] 对于树中的孩子: 如果孩子.标签== 'channel': 对于child中的channel_child: 如果channel_child.标签== 'item': 内容.追加({“内容”:channel_child [2].Text, 'title': channel_child[0].文本}) 返回内容
Prompt_templates = [( 请告诉我{内容}的情感与这个标题:{标题}? 它是非常积极、积极、非常消极、消极还是中性? " 请用这些表达来回答:“非常肯定”, “积极”, “非常消极的”, '消极'或'中性'”), 请从标题{内容}中提取最相关的关键词:{标题}. 在关键字列表前使用“Keywords:”前缀.", “请使用以下内容{内容}对以下内容进行分类,标题{标题}使用这些类别:”+“,".十大正规博彩网站评级(accepted_categories)]模型= 'gpt-3.5-turbo”Def process_llm(input_list: list, prompt_template): """ 使用特定模型创建LLMChain对象 :param input_list一个包含每篇文章的内容和标题的字典列表 :param prompt_template包含内容和标题参数的单个提示模板 """ llm = ChatOpenAI(温度=0,模型=模型) 你也可以用另一种型号. Text-davinci-003比gpt-3贵.5-turbo # llm = OpenAI(temperature=0, model='text-davinci-003') llm_chain = LLMChain( llm = llm, 提示= PromptTemplate.from_template (prompt_template) ) 返回llm_chain.应用(input_list)
def sanitize_categories(文本): 文本=文本.低() 消毒= [] 对于accepted_categories中的cat: 如果在文本中: 消毒.追加(cat) 返回消毒def sanitize_keywords(文本): 文本=文本.低() 文本=文本.替换(“关键词:”、“”).带() 消毒= [re].子(r \.$", "", s.Strip())用于文本中的s.分裂(",")) 返回消毒def categorize_sentiment(文本): 文本=文本.低() 如果在文本中“非常消极”: 返回“非常消极” 文本中的Elif 'negative': 返回“负面” Elif在文本中表示“非常积极”: 回复“非常积极” 文本中的Elif “积极”: 返回"正面" 返回“中性”
Def serialize_results(url, result_df, title, sentiment_counter, categores_counter): """ 将结果转换为Excel工作表或HTML页面. HTML页面还包含计数器信息. :param url RSS提要url :param result_df原始数据与LLM输出的组合 :param title RSS提要URL,其中包含一些修改过的字符 :param sentiment_counter包含情绪信息的计数器 :param categores_counter统计类别的计数器 """ result_df.to_excel (target_folder / f”{标题}.xlsx”) Html_文件 = target_folder/f"{标题}.html” Html_内容 = result_df.to_html(逃避= False) #确保文件是用UTF-8写的 使用open(html_文件, "w", encoding="utf-8")作为文件: 文件.写(html_内容) sentiment_html = generate_sentiment_table(sentiment_counter, "Sentiment") categorories_html = generate_sentiment_table(categorories_counter, "Category") 使用open(html_文件, encoding="utf8")作为f: 内容 = f""" {re.子(r '.+?theguardian.com/pro文件', '', url)} Sentiment Count {sentiment_html} Categories Count {categories_html} {f.read ()} Asian-gaming-platform-rankings-customerservice@deai-romance.net 博彩平台 博彩平台排名 转转 Gambling-website-contactus@wiretapmag.com 腾讯爱玩 游侠网 Asian-gaming-platform-rankings-help@muchodinero4u.com 全球资讯网 博彩app 发型屋 沈阳列表网 新蓝网米秀分享频道 大连康辉国际旅行社 佩蒂股份 人才加 暨南大学教务处 """ 内容=内容.替换('class="dataframe"', 'class="table -striped -hover dataframe"') 使用open(html_文件, "w", encoding="utf8")作为f: f.写(内容)
以下是脚本输出示例,用于以下专栏:
LangChain’s LLM链 提供了一种非常方便的与LLM交互的方式,当您有一个基于列表的输入,您希望对其应用带有参数的预定义LLM提示符时.
吉尔·费尔南德斯,Onepoint咨询公司
在这里注册