ChatGPT – 评估 ChatGPT API
- 让我们从一个简单的 bash 脚本开始
- 让我们使用 Nim 发起一个简单的 API 调用来查询 ChatGPT
- 让我们编写一个简单的应用程序来重用我们上面的可重用模块
- 用于提示和查询 OpenAI API 以使用 ChatGPT 的 Nim CLI 应用程序
让我们从一个简单的 bash 脚本开始
发送至 ChatGPT API 的 JSON 请求示例
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "What is the OpenAI mission?"
}
]
}bash 脚本发送上述有效载荷
#!/bin/bash
set -x #echo on
DATA='{ "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "What is the OpenAI mission?"}]}'
echo $DATA | jq
curl --location --insecure --request POST $OPENAI_URL \
--header "Authorization: Bearer $OPENAI_KEY" \
--header 'Content-Type: application/json' \
--data-raw "$DATA" \
| jq来自 ChatGPT API 的 JSON 响应示例
{
"id": "chatcmpl-6rg9nqm5oyUqQrz5fuaXTR5jqZmDQ",
"object": "chat.completion",
"created": 1678251455,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 14,
"completion_tokens": 100,
"total_tokens": 114
},
"choices": [
{
"message": {
"role": "assistant",
"content": "\n\nAs an AI language model, I can tell you that the OpenAI mission is to develop and promote safe and beneficial artificial intelligence (AI) that benefits humanity. The organization was founded by a group of leading AI experts, including Elon Musk and Sam Altman, and is committed to advancing the understanding and ethical use of AI technology. In addition to conducting research and developing new applications for AI, OpenAI also works to educate the public about the potential benefits and risks of this rapidly evolving field."
},
"finish_reason": null,
"index": 0
}
]
}让我们使用 Nim 发起一个简单的 API 调用来查询 ChatGPT
通过 HTTPS 与 OpenAI API 交互的 Nim 源代码
从环境变量加载 OpenAI API 密钥并设置 API 端点 URL
使用 Nimble(Nim 的包管理器工具)安装 Nim JSON 解析库
nimble install jsony
nimble install cligen| Downloading | https://github.com/treeform/jsony | using | git |
| Verifying | dependencies | for | jsony@1.1.5 |
| Installing | jsony@1.1.5 | ||
| Success: | jsony | installed | successfully. |
| Downloading | https://github.com/c-blake/cligen.git | using | git |
| Verifying | dependencies | for | cligen@1.5.41 |
| Installing | cligen@1.5.41 | ||
| Success: | cligen | installed | successfully. |
# 标准库导入
import std / [asyncdispatch, json, logging, httpclient, macros, os, strformat, strutils, tables]
# 第三方库导入
import jsony
let
loggingFormat = "[$datetime] - $appname - $levelname :::: "
consoleLog = newConsoleLogger(fmtStr=loggingFormat)
errorLog = newFileLogger("errors.log", fmtStr=loggingFormat, levelThreshold=lvlError)
rollingLog = newRollingFileLogger("app-rolling.log", fmtStr=loggingFormat)
addHandler(consoleLog)
addHandler(errorLog)
addHandler(rollingLog)
let
openAiKey = getEnv("OPENAI_KEY")
openAiUrl = getEnv("OPENAI_URL")
info fmt"OpenAI Key was found = {not openAiKey.isEmptyOrWhitespace}"
info fmt"OpenAI URL = {openAiUrl}"| [2023-03-11T11:17:29] | - | nim_src_TDmGqb | - | INFO | :::: | OpenAI | Key | was | found | = | true |
| [2023-03-11T11:17:29] | - | nim_src_TDmGqb | - | INFO | :::: | OpenAI | URL | = | https://api.openai.com/v1/chat/completions |
用于反序列化 ChatGPT 响应的 Nim 类型
type
GptMessage = object
role: string
content: string
GptChoice = object
message: GptMessage
finish_reason: string
index: int
GptUsage = object
prompt_tokens: int
completion_tokens: int
total_tokens: int
GptResponse = object
id: string
`object`: string
created: BiggestInt
model: string
usage: GptUsage
choices: seq[GptChoice]用于启用 SSL 的 Nim 配置文件
d:ssl
d:release用于执行 API 调用并解析 JSON 结果的 Nim 过程
proc onProgressChanged(total, progress, speed: BiggestInt) {.async.} =
debug fmt"Downloaded {progress} of {total}"
debug fmt"Current rate: {speed div 1000} kb/s"
proc asyncPost(
url: string, messages: seq[Table[string, string]],
model = "gpt-3.5-turbo", key = openAiKey
): Future[string] {.async.} =
debug fmt"Messages: {messages}"
let
payload = %*{
"model": model,
"messages": messages,
}
headers = newHttpHeaders({
"Content-Type": "application/json",
"Authorization": fmt"Bearer {key}",
})
client = newAsyncHttpClient()
client.onProgressChanged = onProgressChanged
let response = await client.request(
url, httpMethod = HttpPost,
headers = headers,
body = $payload
)
let
respStatus = response.status
respBody = await response.body
respBodyJson = respBody.parseJson.pretty
debug fmt"Response Status: {respStatus}"
debug fmt"Response Body: {respBodyJson}"
result = respBodyJson
# asyncQA 已导出并可通过 * 后缀公开访问
proc asyncQA*(messages: seq[Table[string, string]], url: string = openAiUrl): Future[string] {.async.} =
## 使用 ChatGPT 进行异步问答
let
answerJson = await url.asyncPost(messages)
answer = answerJson.fromJson(GptResponse)
answerText = answer.choices[0].message.content
result = answerText
debug fmt"Answer: {answerText}"
proc asUsr*(query: string): seq[Table[string, string]] =
result = @[{"role": "user", "content": query}.toTable]
proc asAsstnt*(response: string): seq[Table[string, string]] =
result = @[{"role": "assistant", "content": response}.toTable]使用以下提示通过 OpenAI API 继续查询 ChatGPT:
when isMainModule:
let
messages: seq[Table[string, string]] = @[]
missionQueryFuture = "What is the OpenAI mission?".asUsr.asyncQA
missionQueryFutureFr = "Quelle est la mission de OpenAI?".asUsr.asyncQA
capabilitiesQueryFuture = "What can you do that's Cool?".asUsr.asyncQA
capabilitiesQueryFutureFr = "Qu'est-ce que tu peux faire qui est vraiment Cool?".asUsr.asyncQA
waitFor:
missionQueryFuture and
capabilitiesQueryFuture and
missionQueryFutureFr and
capabilitiesQueryFutureFr
debug fmt"Mission Query Completed = {missionQueryFuture.finished}"
debug fmt"Capabilities Query Completed = {capabilitiesQueryFuture.finished}"
debug fmt"Mission Query FR Completed = {missionQueryFutureFr.finished}"
debug fmt"Capabilities Query FR Completed = {capabilitiesQueryFutureFr.finished}"
let
mission = missionQueryFuture.read
missionFr = missionQueryFutureFr.read
capabilities = capabilitiesQueryFuture.read
capabilitiesFr = capabilitiesQueryFutureFr.read
info fmt"Mission: {mission}"
info fmt"Mission FR: {missionFr}"
info fmt"Capabilities: {capabilities}"
info fmt"Capabilities FR: {capabilitiesFr}"执行 Nim 代码以与 OpenAI API 交互以使用 ChatGPT
#!/bin/bash
set -x #echo on
nim cpp -r chatgpt/chatgpt.nim
rm chatgpt让我们编写一个简单的应用程序来重用我们上面的可重用模块
让我们使用 Nim 向 ChatGPT 发起更多查询
import std / [asyncDispatch, logging, strformat]
import chatgpt / [chatgpt]
when isMainModule:
echo "Further Testing..."
let
whatNextFuture = "What's next?".asUsr.asyncQA
whatNextFutureFr = "Qu'est-ce qui vient après?".asUsr.asyncQA
waitFor:
whatNextFuture and
whatNextFutureFr
let
whatNext = whatNextFuture.read
whatNextFr = whatNextFutureFr.read
info fmt"What's next: {whatNext}"
info fmt"Ce qui vient après: {whatNextFr}"测试 ChatGPT 社交…
#!/bin/bash
set -x #echo on
nim cpp -r chatgpt_social.nim
rm chatgpt_social用于提示和查询 OpenAI API 以使用 ChatGPT 的 Nim CLI 应用程序
让我们在 Nim 中构建一个 ChatGPT CLI 应用程序
[
{ "role": "user", "content": "What is the OpenAI mission?" },
{ "role": "assistant", "content": "The OpenAI mission is to create and promote safe AI (artificial intelligence) that benefits humanity as a whole. They aim to develop and advance artificial intelligence in a way that is safe, transparent, and aligned with human values. OpenAI wants to ensure that the development of AI technology benefits society as a whole, rather than contributing to social inequalities or other negative outcomes. They also believe in promoting collaboration and sharing of resources and knowledge in AI research and development to accelerate progress and avoid duplication of efforts." },
{ "role": "user", "content": "Tell me some more..." },
{ "role": "assistant", "content": "OpenAI was founded in 2015 by a group of prominent individuals from the tech industry, including Elon Musk, Sam Altman, Greg Brockman, and others. Their initial goal was to create AI technology that was beneficial for humanity while simultaneously addressing the potential risks and negative consequences of AI development. One of the main ways that OpenAI aims to achieve its mission is through research and development of advanced AI systems that can be trained to perform complex tasks and solve real-world problems. They also advocate for ethical and socially responsible approaches to AI development and deployment. Additionally, OpenAI has created several tools and resources that are available to the public, including language models like GPT-3 and machine learning frameworks like PyTorch. They also host conferences and workshops to foster collaboration and sharing of knowledge among AI researchers and developers. Overall, OpenAI's mission is to create and promote AI for the greater good, with a focus on transparency, fairness, and safety." },
]Tell me some more...import std / [asyncDispatch, json, logging, os, sequtils, strformat, strutils, tables]
import chatgpt / [chatgpt]
proc processArgs(query = "user_query.txt", role = "user", prompts = "prompts.json") =
let progName = split(getAppFileName(), "/")[getAppFileName().count("/")]
if query != "":
info fmt"Role: {role} -- Query: {query}"
let
prompts = fmt"""{prompts.readFile}""".parseJson.to(seq[Table[string, string]])
queryMsg = query.readFile.asUsr
messages = concat(prompts, queryMsg)
response = waitFor messages.asyncQA
debug fmt"Prompts: {prompts}"
info fmt"Response: {response}"
else:
error fmt"ERROR:::: {progName} needs a query"
when isMainModule:
import cligen
echo "ChatGPT CLI..."
dispatch(processArgs)API Calls are essential to communitcating with remote services in Software Engineering.
JSON Payloads are the preferred choice as information exchange format.[
{ "role": "user", "content": "You will be my translator from English to French today, and as my translator you will translate everything I say to English keeping in mind that I may use some Technical terms pertain to programming, software engineering, datascience, and devops" },
{ "role": "assistant", "content": "Yes, I am ready to assist you and translate your English content into the most appropriate French equivalent in a way that accounts for the proper terms to use with regards to programming, software engineering, datascience, and devops." },
]#!/bin/bash
set -x #echo on
nim cpp -r chatgpt_cli.nim $@
rm chatgpt_cli
