admin管理员组

文章数量:1631725

目录

一、准备

1.node.js

2.阿里云函数计算

3.两行命令实现部署

第一步:初始化项目。

第二步:一键部署。

二、使用代理访问API


一、准备

1.node.js

npm安装:
$ npm install @serverless-devs/s -g

yarn安装:
$ yarn global add @serverless-devs/s

2.阿里云函数计算

1、通过该链接(https://usercenter.console.aliyun/#/manage/ak ),获取阿里云的AccessKey。把这里获取的AccessKey ID和AccessKey Secret记下来,下面马上要用。
2、为Serverless Devs Tool配置阿里云的AccessKey


$ s config add 
? Please select a provider: Alibaba Cloud (alibaba)
🧭 Refer to the document for alibaba key:  http://config.devsapp/account/alibaba
? AccessKeyID 此处填写AccessKeyID
? AccessKeySecret 此处填写AccessKeySecret
? Please create alias for key pair. If not, please enter to skip alibaba-access

Alias: alibaba-access
AccountID: 自动获取AccountID
AccessKeyID: 此处填写AccessKeyID
AccessKeySecret: 此处填写AccessKeySecret

✔ Configuration successful

3.两行命令实现部署

在完成了上面的准备工作之后,接下来只需要两行命令即可完成代理服务的部署

第一步:初始化项目。

执行命令s init openai-proxy,创建openai-proxy项目。

$ s init openai-proxy

🚀  More applications: https://registry.serverless-devs

? Please input your project name (init dir) openai-proxy
✔ file decompression completed
创建应用所在的地区
? 地域 us-west-1
? please select credential alias aliyun-key

🏄‍  Thanks for using Serverless-Devs
👉  You could [cd /Users/zhaiyongchao/IdeaProjects/test/openai-proxy] and enjoy your serverless journey!
🧭️  If you need help for this example, you can use [s -h] after you enter folder.
💞  Document ❤ Star: https://github/Serverless-Devs/Serverless-Devs
🚀  More applications: https://registry.serverless-devs


会有几个选项,一直回车就好,如果想修改也行,自己根据需要选择即可。

第二步:一键部署。

进到初始化好的openai-proxy目录下,然后执行命令s deploy直接部署到阿里云上。不要怀疑,就是这么丝滑,一键完成!

$ s deploy

📑 Config check:
Online status => Target Status
  routeConfig: [
    0: {
    + qualifier: undefined
    }
  ]

? Domain [auto] was changed, please confirm before deployment:
    * You can also specify to use local configuration through --use-local during deployment)  use local
✔ Generated auto custom domain...
✔ Generated custom domain (3.78s)
⠙ Generated auto custom domain...
注意:当前域名并非阿里云官方域名,由 CNCF Project Serverless Devs 社区提供,仅供测试使用。
Note: The current domain name is not the official domain name of Alibaba Cloud. It is provided by the CNCF Project Serverless Devs community for testing purposes only
✔ Generated domain (23.38s)
✔ Checking Service, Function, Triggers (1.53s)
✔ Creating Service, Function, Triggers (9.57s)
✔ Creating custom domain (1.7s)

Tips for next step
======================
* Display information of the deployed resource: s info
* Display metrics: s metrics
* Display logs: s logs
* Invoke remote function: s invoke
* Remove Service: s remove service
* Remove Function: s remove function
* Remove Trigger: s remove trigger
* Remove CustomDomain: s remove domain


...

 

在部署完成之后,最后面会有上图这样的一段输出,记住红线部分的域名,这是可以直接访问。你可以尝试访问一下,如果返回如下信息,说明部署成功了。

{
  "error": {
    "message": "Invalid URL (GET /)",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

二、使用代理访问API

网上有很多开源项目可以使用,可以使用我所引用的原文链接中的项目进行测试

这里我写一个简单的.py文件用于测试。

import requests

# 设置 API 地址和 API Key
api_url = "<你的域名>/v1/chat/completions"
api_key = "<你的API密钥>"

# 设置请求头
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
}

# 设置请求参数 json 格式
data = {
    "model":"gpt-3.5-turbo",
    "messages":[
        
         {"role": "system", "content": "你是一个编程助手,能够帮我编写C#代码,并且给我提示"},
         {"role": "user", "content": "你好,你叫什么名字"},
         {"role": "assistant", "content": "你好,我是一个语言模型AI,没有实体名字,你可以随意称呼我。"},
         {"role": "user", "content": "1+1=?"},
    ],

    "temperature": 0.5,
    "max_tokens": 500
}

# 发送 POST 请求
response = requests.post(api_url, headers=headers, json=data)

# 获取响应内容
result = response.json()

# 输出响应内容
print(result)

 

学习引用---原文链接:https://blog.csdn/zyq880625/article/details/130092712

本文标签: 函数chatGPTAPI