国内使用ChatGPT

作者: 梅西爱骑车 | 来源:发表于2024-02-11 08:49 被阅读0次

    访问ChatGPT困难重重,特别是调用其API,想付费都无比艰难。所以这里推荐去申请微软的Azure(portal.azure.com/)里面的大语言模型,可以用国内的信用卡按量付费。

    里面关键是进入“模型部署”部署模型,然后去“密钥和终结点”找到自己的密钥和终结点。


    首先要安装python-dotenv和openai模块。使用python-dotenv库来读取.env文件初始化环境变量。

    !pip install python-dotenv
    
    !pip install openai
    

    在你的项目目录中创建一个.env文件,并将你的OpenAI API密钥、终结点(在“密钥和终结点”里面找)等添加到该文件中。这里.env文件的概念和Docker Compose用的.env文件是一样的。

    .env文件的内容如下:

    OPENAI_API_BASE=https://endpoint.openai.azure.com/ #终结点
    
    OPENAI_API_KEY=youropenapikey #密钥
    
    OPENAI_API_TYPE=azure
    
    OPENAI_API_VERSION=2023-07-01-preview
    
    

    使用以下代码加载.env文件中的环境变量:

    import openai
    
    from dotenv import load_dotenv, find_dotenv
    
    load_dotenv(find_dotenv()) # read local .env file
    
    deployment = "gpt-35-turbo" # 就是上面的部署名
    
    model = "gpt-3.5-turbo"
    

    通过直接调用Api的方式来进行文本翻译


    我们先通过“传统”的调用Api的方式来完成程序的功能:将海盗英语翻译成“优雅尊重的美式英语”。

    def get_completion(prompt, model="gpt-3.5-turbo", engine="gpt-35-turbo"):
    
     messages = [{"role": "user", "content": prompt}]
    
     response = openai.ChatCompletion.create(
    
     model=model,
    
     engine=engine, #使用Azure的GPT-3.5模型要用这个
    
     messages=messages,
    
     temperature=0, 
    
     )
    
     return response.choices[0].message["content"]
    
    customer_email = """
    
    Arrr, I be fuming that me blender lid \
    
    flew off and splattered me kitchen walls \
    
    with smoothie! And to make matters worse,\
    
    the warranty don't cover the cost of \
    
    cleaning up me kitchen. I need yer help \
    
    right now, matey!
    
    """
    
    style = """American English in a calm and respectful tone"""
    
    prompt = f"""Translate the text \
    
    that is delimited by triple backticks
    
    into a style that is {style}.
    
    text: ```{customer_email}```
    
    """
    
    print(prompt)
    
    response = get_completion(prompt)
    
    print(response)
    

    这里使用Python的fstring来实现提示的“参数化”。在Python世界,fstring很常用,就是多了个f感觉不太美观,不如直接用groovy的gstring的形式。

    最终传给模型的Prompt是这样的:

    Translate the text that is delimited by triple backticks
    
    into a style that is American English in a calm and respectful tone.
    
    text: \```
    
    Arrr, I be fuming that me blender lid flew off and splattered me kitchen walls with smoothie! And to make matters worse,the warranty don't cover the cost of cleaning up me kitchen. I need yer help right now, matey!
    
    \```
    
    

    这里采用了一个小技巧来明确告诉模型要翻译的文本:把翻译的文本用三重引号(triple backticks)来括住。

    模型运行返回了很漂亮的英语:I'm really frustrated that my blender lid flew off and made a mess of my kitchen walls with smoothie! And to add to my frustration, the warranty doesn't cover the cost of cleaning up my kitchen. I could really use your help at this moment, my friend.

    相关文章

      网友评论

        本文标题:国内使用ChatGPT

        本文链接:https://www.haomeiwen.com/subject/pqmradtx.html