ChatGPT – ChatGPT API का मूल्यांकन
- चलिए एक साधारण bash स्क्रिप्ट से शुरुआत करते हैं
- चलिए Nim का उपयोग करके ChatGPT को क्वेरी करने के लिए एक साधारण API कॉल करते हैं
- चलिए ऊपर दिए गए हमारे पुन: प्रयोज्य मॉड्यूल का पुन: उपयोग करने के लिए एक साधारण ऐप लिखें
- ChatGPT के लिए OpenAI API को प्रॉम्प्ट और क्वेरी करने हेतु 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" \
| jqChatGPT 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 का उपयोग करके ChatGPT को क्वेरी करने के लिए एक साधारण API कॉल करते हैं
HTTPS के माध्यम से OpenAI API के साथ इंटरैक्ट करने के लिए Nim सोर्स कोड
पर्यावरण से OpenAI API की को लोड करें और API एंडपॉइंट URL सेटअप करें
Nimble का उपयोग करके एक Nim JSON पार्सिंग लाइब्रेरी इंस्टॉल करें, जो Nim पैकेज मैनेजर टूल है
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 प्रतिक्रिया को डीसीरियलाइज़ (deserialize) करने के लिए 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:releaseAPI कॉल निष्पादित करने और 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}"ChatGPT के लिए OpenAI API के साथ इंटरैक्ट करने के लिए Nim कोड निष्पादित करें
#!/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_socialChatGPT के लिए OpenAI API को प्रॉम्प्ट और क्वेरी करने हेतु 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
