1 Star 3 Fork 0

ChangweiZhang / openai-arkts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

openai-arkts

介绍

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

使用方法

使用openai.com API

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",
    }
)

使用Azure OpenAI 聊天API

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)));
})

Embedding接口

 import { OpenAI } from '@changwei/openai'

openaiClient: OpenAI = new OpenAI(
    "your sk-xxx"
)
openaiClient.embeddings.create({
                input: "your input text",
                model: "text-embedding-ada-002"
              })

访问原始响应数据(例如header)

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);
        
})

Completion API 文本补全

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同学

抖音/西瓜 @梦断代码

求点赞 求关注 求分享

MIT License Copyright (c) 2024 ChangweiZhang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

OpenAI SDK鸿蒙原生版,采用ArkTS编写,支持ChatCompletiong,Embedding等接口实现,调用方式和python SDK一致 展开 收起
TypeScript 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/changweizhang/openai-arkts.git
git@gitee.com:changweizhang/openai-arkts.git
changweizhang
openai-arkts
openai-arkts
master

搜索帮助