conda环境安装,miniconda也可以
下载: MiniConda官网
安装:略 。可以参考
安装Python:Python> 3.10
conda create -n LLM_Learn python=3.10.1
conda activate LLM_Learn
// 添加清华镜像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
// 安装所需依赖包
conda install openai, langchain, python-dotenv
conda install -y jupyter
API KEY获取先省略,但是这一步很重要,可能需要科学上网
.env文件可以自己创建,只要写入如图的格式:
OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxx
导入模块
import openai
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.getenv('OPENAI_API_KEY')
写一个生成内容的函数
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
接下来就是激动人心的时刻:
prompt = "请写首诗"
response = get_completion(prompt)
print(response)