代码拉取完成,页面将自动刷新
OpenAI SDK鸿蒙原生版,采用ArkTS编写,支持ChatCompletiong,Embedding等接口实现,调用方式和python SDK一致
ohpm install @changwei/openai
OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包
可以在 platform.openai.com 上找到 REST API 文档。 如果使用Azure OpenAI服务,请访问:https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart
import { OpenAI } from '@changwei/openai'
openaiClient: OpenAI = new OpenAI(
"your sk-xxx"
)
openaiClient.chat.completions.create(
{
messages: [
{
"role": "user",
"content": "Say this is a test",
}
],
model: "gpt-3.5-turbo",
}
)
import { AzureOpenAI } from '@changwei/openai'
openaiClient: AzureOpenAI = new AzureOpenAI(
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource
"your endpoint",
// https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning
"2023-07-01-preview",
"your api-key"
)
openaiClient.chat.completions.create(
{
messages: [
{
"role": "user",
"content": "Say this is a test",
}
],
model: "deployment-name" // e.g. gpt-35-instant
}
)
因为目前axios还不支持SSE模式,此处流式实现有延时。此处以openai.com接口调用为例
import { OpenAI,ChatCompletionChunk } from '@changwei/openai'
openaiClient: OpenAI = new OpenAI(
"your sk-xxx"
)
openaiClient.chat.completions.create(
{
messages: [
{
"role": "user",
"content": "Say this is a test",
stream: true
}
],
model: "gpt-3.5-turbo",
}
)
.then((response) => {
console.log(JSON.stringify((response)));
let resp = response as ChatCompletionChunk[]
let output: string = ""
for (let chunck of resp) {
if (chunck?.choices[0]?.delta?.content) {
output += chunck.choices[0].delta.content as string
}
}
console.log(output);
})
.catch((error) => {
console.log(JSON.stringify((error)));
})
import { OpenAI } from '@changwei/openai'
openaiClient: OpenAI = new OpenAI(
"your sk-xxx"
)
openaiClient.embeddings.create({
input: "your input text",
model: "text-embedding-ada-002"
})
import { OpenAI } from '@changwei/openai'
openaiClient: OpenAI = new OpenAI(
"your sk-xxx"
)
openaiClient.chat.completions.with_raw_response.create(
{
messages: [
{
"role": "user",
"content": "Say this is a test",
}
],
model: "gpt-3.5-turbo",
}
)
.then((response)=>{
console.log(response.headers["content-type"]);
let completion = response.parse();
console.log(completion);
})
openaiClient.completions.create({
model: "gpt-3.5-turbo-instruct",
prompt: "Say this is a test",
max_tokens: 7,
temperature: 0
})
在下述版本验证通过: DevEco Studio: 4.0.0.600, SDK: API9
使用过程中发现任何问题都可以提Issue 给我 。
本项目基于 MIT ,请自由地享受和参与开源。
B站 @Changwei同学
抖音/西瓜 @梦断代码
求点赞 求关注 求分享
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。