LangChain与大模型的学习

这里写目录标题

问题记录

1、库的版本问题

powershell 复制代码
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)')))

解决办法

powershell 复制代码
降低urllib3的版本 <= 1.25.11

pip install urllib3==1.25.11

实例记录

1、公司名生成

python 复制代码
# 来源于LangChain中文网
#-*- coding:utf-8 -*-
# 实例1:构建一个基于公司产品生成公司名称的服务
import os
os.environ["OPENAI_API_KEY"] = "......"
from langchain.llms import OpenAI

llm = 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 提示模板的使用

python 复制代码
# 实例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?

3LLM Chain

python 复制代码
# 实例3:在多步骤的工作流中组合 LLM 和提示
import os
os.environ["OPENAI_API_KEY"] = "......"
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

llm = 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!

参考资料

1、LangChain中文网

2、python 关于Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1131)')

相关推荐
吃好睡好便好26 分钟前
伽利略·伽利雷的故事
学习
GEO从入门到精通27 分钟前
为什么要学习GEO?
人工智能·学习
小趴菜不能喝31 分钟前
LangChain 开发Agent基础
langchain
云姜.1 小时前
如何快速使用Langchain上手编程
python·langchain
星幻元宇VR1 小时前
VR消防安全体验屋|沉浸式科技助力消防安全科普
人工智能·科技·学习·安全·vr
小+不通文墨1 小时前
在树莓派中部署emqx
经验分享·笔记·单片机·学习
情绪总是阴雨天~1 小时前
深度解析:LangChain、Agent、RAG、FC、ReAct、LangGraph、A2A、MCP — 区别、联系与全景图
python·langchain·agent·rag·langgraph·mcp·a2a
赢乐2 小时前
AI大模型学习笔记:LangChain核心组件-工具(Tools)
langchain·大模型·agent·function_call·工具(tools)·tool装饰器·定义工具
玄米乌龙茶1232 小时前
LLM 应用开发学习笔记:System Prompt 设计、注入风险与成本优化
笔记·学习·prompt
爱喝水的鱼丶2 小时前
SAP-ABAP:数据类型与数据对象(8篇) 第四篇:关系映射篇——从类型定义到对象实例的转化逻辑
开发语言·数据库·学习·sap·abap