LangChain与大模型的学习ing

news/2025/3/15 9:14:40/

大模型的菜鸟初学习

  • 一、问题记录
    • 1、库的版本问题
  • 二、实例记录
    • 1、公司名生成
    • 2、提示模板的使用
    • 3、LLM Chain
    • 4、LLMMemory
    • 5、聊天语言API
  • 参考资料

一、问题记录

1、库的版本问题

openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443): Max retries exceeded with url: /v1/completions (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1129)')))

解决办法

降低urllib3的版本 <= 1.25.11pip install urllib3==1.25.11

二、实例记录

1、公司名生成

# 来源于LangChain中文网
#-*- coding:utf-8 -*-
# 实例1:构建一个基于公司产品生成公司名称的服务
import os
os.environ["OPENAI_API_KEY"] = "……"
from langchain.llms import OpenAIllm = OpenAI(temperature=0.9)
text = "What would be a good company name for a company that makes colorful socks?"
print(llm(text))# 输出:Rainbow Rascals Socks.

我的第一个调用实例,感觉还是很神奇的

“temperature” : OpenAI的API有效载荷中,"temperature"选项是一个控制语言模型输出的随机性或创造性的参数。当使用语言模型生成文本时,它通常会输出根据输入和先前训练数据确定为最可能的单词或词序列。然而,增加输出的随机性可以帮助模型创建更具创意和有趣的输出。"temperature"选项实际上控制着随机性的程度。将温度设置为较低的值将导致输出更可预测和重复,而较高的温度会导致更多种类和不可预测的输出。例如,将温度设置为0.5将导致较保守的输出,而温度为1将创建更富创意和自发的输出。需要注意的是,理想的温度值将取决于具体的任务和上下文,因此可能需要一些实验来找到适合您需要的正确值。

2、提示模板的使用

# 实例2:提示模板的使用
prompt = PromptTemplate(input_variables=["product"],template="What is a good name for a company that makes {product}?",
)
print(prompt.format(product="colorful socks"))# 输出:What is a good name for a company that makes colorful socks?

3、LLM Chain

# 实例3:在多步骤的工作流中组合 LLM 和提示
import os
os.environ["OPENAI_API_KEY"] = "……"
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChainllm = OpenAI(temperature=0.9)prompt = PromptTemplate(input_variables=["product"],template="What is a good name for a company that makes {product}?",
)chain = LLMChain(llm=llm, prompt=prompt)print(chain.run("colorful socks"))# 输出:Sock Pop!

4、LLMMemory

实例4:带记忆的多轮会话import os
os.environ["OPENAI_API_KEY"] = "……"from langchain import OpenAI, ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)#verbose=True 这样我们就可以看到提示符HumanSent=""while HumanSent!= "q":HumanSent = input("input:")output = conversation.predict(input=HumanSent)print(output)

默认情况下, ConversationChain 有一个简单的内存类型,它记住所有以前的输入/输出,并将它们添加到传递的上下文中。

5、聊天语言API

import os
os.environ["OPENAI_API_KEY"] = "……"from langchain.chat_models import ChatOpenAI
from langchain.schema import (AIMessage,HumanMessage,SystemMessage
)
chat = ChatOpenAI(temperature=0)batch_messages = [[SystemMessage(content="You are a helpful assistant that translates English to Chinese."),HumanMessage(content="Translate this sentence from English to Chinese. I love programming.")],[SystemMessage(content="You are a helpful assistant that translates English to Chinese."),HumanMessage(content="Translate this sentence from English to Chinese. I love artificial intelligence.")],
]
result = chat.generate(batch_messages)print(result)
result.llm_output['token_usage']'''
generations=[[ChatGeneration(text='我喜欢编程。', generation_info={'finish_reason': 'stop'}, message=AIMessage(content='我喜欢编程。', additional_kwargs={}, example=False))],[ChatGeneration(text='我喜欢人工智能。',generation_info={'finish_reason': 'stop'},message=AIMessage(content='我喜欢人工智能。', additional_kwargs={}, example=False))]]
llm_output={'token_usage': {'prompt_tokens': 69, 'completion_tokens': 19, 'total_tokens': 88},'model_name': 'gpt-3.5-turbo'} 
run=[RunInfo(run_id=UUID('5a2161dd-d623-4d0c-8c0a-0c95df966ca1')),RunInfo(run_id=UUID('e42fad23-306a-437f-868c-7e5783bedc4b'))]
'''

参考资料

1、LangChain中文网
2、python 关于Caused by SSLError(SSLEOFError(8, ‘EOF occurred in violation of protocol (_ssl.c:1131)‘)


http://www.ppmy.cn/news/1018616.html

相关文章

适配器模式-java实现

意图 复用已经存在的接口&#xff0c;与所需接口不一致的类。即将一个类&#xff08;通常是旧系统中的功能类&#xff09;&#xff0c;通过适配器转化成另一个接口的实现。&#xff08;简单来说&#xff0c;就是复用旧系统的功能&#xff0c;去实现新的接口&#xff09; 我们举…

VUE学习七收集表单数据

<!DOCTYPE html> <html><head><meta charset"UTF-8" /><title>收集表单数据</title><script type"text/javascript" src"../js/vue.js"></script></head><body><!-- 收集表单数…

springboot启动忽略某些类

springboot启动忽略某些类 描述解决方案单拉一个提交&#xff0c;把所有的涉及kafka消费的都不注入容器通过配置ComponentScan的excludeFilters配置了不生效后续处理改之前改之后解释 总结 拆分环境 感触解决实现demo参考 描述 目前我这的开发环境和测试环境数据库是两份&#…

【数据结构与算法】Vue3实现选择排序动画效果与原理拆解

系列文章目录 删除有序数组中的重复项 JavaScript实现选择排序 文章目录 系列文章目录1、选择排序的原理1.1、选择排序的基本步骤1.2、拆解思路 2、动画演示原理3、代码实现4、优化后的选择排序5、用Vue3实现选择排序的动画效果&#xff08;第二部分的动画效果图&#xff09; …

Weblogic未授权远程代码执行漏洞 (CVE-2023-21839)

前言&#xff1a; Weblogic 允许远程用户在未经授权的情况下通过IIOP/T3进行JNDI lookup 操作&#xff0c;当JDK版本过低或本地存在javaSerializedData时&#xff0c;这可能会导致RCE漏洞。 0x00 环境设置 此次实验&#xff0c;我们使用P神的vulhub。启动环境&#xff1a; cd…

leetcode410. 分割数组的最大值 动态规划

hard:https://leetcode.cn/problems/split-array-largest-sum/ 给定一个非负整数数组 nums 和一个整数 m &#xff0c;你需要将这个数组分成 m 个非空的连续子数组。 设计一个算法使得这 m 个子数组各自和的最大值最小。 示例 1&#xff1a;输入&#xff1a;nums [7,2,5,1…

Windows 安装 pandoc 将 jupyter 导出 pdf 文件

Windows 安装 pandoc 将 jupyter 导出 pdf 文件 1. 下载 pandoc 安装文件2. 安装 pandoc3. 安装 nbconvert4. 使用 pandoc 1. 下载 pandoc 安装文件 访问 https://github.com/jgm/pandoc/releases&#xff0c;下载最新版安装文件&#xff0c;例如&#xff0c;3.1.6.1 版&#…

javaScript:如何获取html中的元素对象

目录 前言&#xff1a; 方法 1.通过id获取元素 2.通过标签名获取元素 3.通过类名class获取元素 获取body的方法 1.document.getElementsByTagName(body)[0] 2.document.body 相关代码 前言&#xff1a; 通过获取HTML中的元素对象&#xff0c;JavaScript可以对网页进行动…