Talk-Bot开放接口

对外接口


调用库(Libraries)

<p>[TOC]</p> <h1>调用案例</h1> <p>官方目前只提供两种语言的调用库,分别为:Node.js与Python,其它语言调用需要自己写代码,或者使用别人写好的,我们给大家整理了一些社区库供大家参考。</p> <h2>Node.js 版本,基于官方提供的OpenAI库</h2> <pre><code>const {OpenAI} = require('openai') const openai = new OpenAI({ apiKey: '你的密钥', baseURL: 'https://api.hellogptworld.com/v1' }) async function main() { const completion = await openai.chat.completions.create({ messages: [{ role: 'user', content: 'Hello, Nice to meet you' }], model: 'gpt-3.5-turbo', }); console.log(completion); } main();</code></pre> <h2>Python 版本,基于官方提供的OpenAI库,非流式正常输出,流式的暂时不支持,建议使用下边的requests库版本</h2> <h3>非流式输出</h3> <pre><code>from openai import OpenAI client = OpenAI( api_key = '你的密钥', base_url = 'https://api.hellogptworld.com/v1' ) completion = client.chat.completions.create( model = &amp;quot;gpt-3.5-turbo&amp;quot;, messages = [{&amp;quot;role&amp;quot;: &amp;quot;user&amp;quot;, &amp;quot;content&amp;quot;: &amp;quot;Hello, Nice to meet you&amp;quot;}], ) print(completion)</code></pre> <h3>流式输出</h3> <pre><code>from openai import OpenAI client = OpenAI( api_key = '你的密钥', base_url = 'https://api.hellogptworld.com/v1' ) stream = client.chat.completions.create( model=&amp;quot;gpt-3.5-turbo&amp;quot;, messages=[{&amp;quot;role&amp;quot;: &amp;quot;user&amp;quot;, &amp;quot;content&amp;quot;: &amp;quot;Hello, Nice to meet you&amp;quot;}], stream=True, ) for chunk in stream: print(chunk) </code></pre> <h2>Python 版本,基于requests库</h2> <h3>非流式输出</h3> <pre><code>import requests data = { &amp;quot;model&amp;quot;: &amp;quot;gpt-3.5-turbo&amp;quot;, &amp;quot;messages&amp;quot;: [{&amp;quot;role&amp;quot;: &amp;quot;user&amp;quot;, &amp;quot;content&amp;quot;: &amp;quot;Hello, Nice to meet you&amp;quot;}], &amp;quot;stream&amp;quot;: False } url = &amp;quot;https://api.hellogptworld.com/v1/chat/completions&amp;quot; headers = { &amp;quot;Content-Type&amp;quot;: &amp;quot;application/json&amp;quot;, &amp;quot;Authorization&amp;quot;: &amp;quot;Bearer 你的密钥&amp;quot; } response = requests.post(url, json=data, headers=headers) print(response.text)</code></pre> <h3>流式输出</h3> <pre><code>import requests data = { &amp;quot;model&amp;quot;: &amp;quot;gpt-3.5-turbo&amp;quot;, &amp;quot;messages&amp;quot;: [{&amp;quot;role&amp;quot;: &amp;quot;user&amp;quot;, &amp;quot;content&amp;quot;: &amp;quot;Hello, Nice to meet you&amp;quot;}], &amp;quot;stream&amp;quot;: True } url = &amp;quot;https://api.hellogptworld.com/v1/chat/completions&amp;quot; headers = { &amp;quot;Content-Type&amp;quot;: &amp;quot;application/json&amp;quot;, &amp;quot;Authorization&amp;quot;: &amp;quot;Bearer 你的密钥&amp;quot; } response = requests.post(url, json=data, headers=headers) for line in response.iter_lines(): if line: print(line.decode('UTF-8'))</code></pre> <h2>Java 版本</h2> <h3>实体封装、依赖jar包引入</h3> <pre><code>&amp;lt;dependency&amp;gt; &amp;lt;groupId&amp;gt;cn.hutool&amp;lt;/groupId&amp;gt; &amp;lt;artifactId&amp;gt;hutool-all&amp;lt;/artifactId&amp;gt; &amp;lt;version&amp;gt;5.8.22&amp;lt;/version&amp;gt; &amp;lt;/dependency&amp;gt;</code></pre> <pre><code>ChatCompletion chat = new ChatCompletion(); chat.setModel(&amp;quot;gpt-3.5-turbo&amp;quot;); chat.setStream(false); List&amp;lt;Message&amp;gt; messages = new ArrayList&amp;lt;&amp;gt;(); Message message = new Message(); message.setRole(&amp;quot;user&amp;quot;); message.setContent(&amp;quot;Hello, Nice to meet you&amp;quot;); messages.add(message); chat.setMessages(messages);</code></pre> <h3>Http 非流式方式</h3> <pre><code>String requestBody = JSONUtil.toJsonStr(chat); String body = HttpRequest.post(&amp;quot;https://api.hellogptworld.com/v1/chat/completions&amp;quot;) .bearerAuth(&amp;quot;你的密钥&amp;quot;) .contentType(ContentType.JSON.getValue()) .timeout(30000) .body(requestBody) .execute().body(); System.out.println(body);</code></pre> <h3>Http 流式请求方式</h3> <pre><code>String requestBody = JSONUtil.toJsonStr(chat); InputStream inputStream = HttpRequest.post(&amp;quot;https://api.hellogptworld.com/v1/chat/completions&amp;quot;) .bearerAuth(&amp;quot;你的密钥&amp;quot;) .contentType(ContentType.JSON.getValue()) .timeout(30000) .body(requestBody) .execute().bodyStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); }</code></pre> <h3>SseEmitter 流式请求方式</h3> <pre><code>&amp;lt;dependency&amp;gt; &amp;lt;groupId&amp;gt;com.squareup.okhttp3&amp;lt;/groupId&amp;gt; &amp;lt;artifactId&amp;gt;okhttp-sse&amp;lt;/artifactId&amp;gt; &amp;lt;version&amp;gt;3.14.9&amp;lt;/version&amp;gt; &amp;lt;/dependency&amp;gt;</code></pre> <pre><code>@PostMapping(&amp;quot;/stream&amp;quot;) public SseEmitter stream(@RequestBody ChatCompletion chat) { SseEmitter sseEmitter = new SseEmitter(-1L); SseStreamHandler streamHandler = new SseStreamHandler(sseEmitter); EventSource.Factory factory = OpenAiUtils.init(); chat.setUser(NanoId.randomNanoId()); String requestBody = JSONUtil.toJsonStr(chat); Request res = OpenAiUtils.requestChat(requestBody); factory.newEventSource(res, streamHandler); return sseEmitter; }</code></pre> <pre><code>public class OpenAiUtils { public static EventSource.Factory init() { OkHttpClient.Builder client = new OkHttpClient.Builder(); client.connectTimeout(300000, TimeUnit.MILLISECONDS); client.writeTimeout(300000, TimeUnit.MILLISECONDS); client.readTimeout(300000, TimeUnit.MILLISECONDS); return EventSources.createFactory(client.build()); } public static Request requestChat(String requestBody) { Request.Builder builder = new Request.Builder() .url(&amp;quot;https://api.hellogptworld.com/v1/chat/completions&amp;quot;) .post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody)) .header(Header.AUTHORIZATION.getValue(), &amp;quot;Bearer 你的密钥&amp;quot;); return builder.build(); } }</code></pre> <pre><code>@RequiredArgsConstructor public class SseStreamHandler extends EventSourceListener { private final SseEmitter sseEmitter; @Override public void onOpen(EventSource eventSource, Response response) { System.out.println(&amp;quot;sseEmitter onOpen start&amp;quot;); } @Override public void onClosed(EventSource eventSource) { eventSource.cancel(); } public void onMsg(String message) { sseEmitter.send(message); } public void onComplete() { sseEmitter.complete(); } @Override public void onEvent(EventSource eventSource, String id, String type, String res) { System.out.println(&amp;quot;sseEmitter onEvent res:{}&amp;quot;, res); } @Override public void onFailure(EventSource eventSource, Throwable throwable, Response res) { System.out.println(&amp;quot;sseEmitter onFailure res:{}&amp;quot;, res); }</code></pre> <h1>其它语言社区库</h1> <p>下面这些库由更广泛的开发人员社区构建和维护,仅供参考。请注意,Talk-Bot不会验证这些项目的正确性或安全性。</p> <h2>C# / .NET[​](<a href="https://openai.xiniushu.com/docs/libraries#c--net">https://openai.xiniushu.com/docs/libraries#c--net</a> &quot;C# / .NET的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/betalgo/openai">Betalgo.OpenAI.GPT3</a> by <a href="https://github.com/betalgo">Betalgo</a></li> </ul> <h2>Crystal[​](<a href="https://openai.xiniushu.com/docs/libraries#crystal">https://openai.xiniushu.com/docs/libraries#crystal</a> &quot;Crystal的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/sferik/openai-crystal">Openai-Crystal</a> by <a href="https://github.com/sferik">Sferik</a></li> </ul> <h2>Go[​](<a href="https://openai.xiniushu.com/docs/libraries#go">https://openai.xiniushu.com/docs/libraries#go</a> &quot;Go的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/sashabaranov/go-gpt3">Go-GPT3</a> by <a href="https://github.com/sashabaranov">sashabaranov</a></li> </ul> <h2>Java[​](<a href="https://openai.xiniushu.com/docs/libraries#java">https://openai.xiniushu.com/docs/libraries#java</a> &quot;Java的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/TheoKanning/openai-java">openai-java</a> by <a href="https://github.com/TheoKanning">Theo Kanning</a></li> <li><a href="https://github.com/PlexPt/chatgpt-java">chatgpt-java</a> by <a href="https://github.com/PlexPt">PlexPt</a></li> <li><a href="https://github.com/Grt1228/chatgpt-java">chatgpt-java</a> by <a href="https://github.com/Grt1228">Grt1228</a></li> </ul> <h2>Kotlin[​](<a href="https://openai.xiniushu.com/docs/libraries#kotlin">https://openai.xiniushu.com/docs/libraries#kotlin</a> &quot;Kotlin的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/Aallam/openai-kotlin">openai-kotlin</a> by <a href="https://github.com/Aallam">Mouaad Aallam</a></li> </ul> <h2>Node.js[​](<a href="https://openai.xiniushu.com/docs/libraries#nodejs">https://openai.xiniushu.com/docs/libraries#nodejs</a> &quot;Node.js的直接链接&quot;)</h2> <ul> <li><a href="https://www.npmjs.com/package/openai-api">openai-api</a> by <a href="https://github.com/Njerschow">Njerschow</a></li> <li><a href="https://www.npmjs.com/package/openai-api-node">OpenAI-API-Node</a> by <a href="https://github.com/erlapso">Erlapso</a></li> <li><a href="https://www.npmjs.com/package/gpt-x">GPT-X</a> by <a href="https://github.com/ceifa">CEIFA</a></li> <li><a href="https://www.npmjs.com/package/gpt3">GPT3</a> by <a href="https://github.com/poteat">Poteat</a></li> <li><a href="https://www.npmjs.com/package/gpts">GPTS</a> by <a href="https://github.com/thencc">thencc</a></li> <li><a href="https://www.npmjs.com/package/@dalenguyen/openai">@dalenguyen/OpenAI</a> by <a href="https://github.com/dalenguyen">Dalenguyen</a></li> <li><a href="https://github.com/tectalichq/public-openai-client-js">Tectalic/OpenAI</a> by <a href="https://tectalic.com/">tectalic</a></li> </ul> <h2>PHP[​](<a href="https://openai.xiniushu.com/docs/libraries#php">https://openai.xiniushu.com/docs/libraries#php</a> &quot;PHP的直接链接&quot;)</h2> <ul> <li><a href="https://packagist.org/packages/orhanerday/open-ai">Orhanerday/Open-AI</a> by <a href="https://github.com/orhanerday">Orhanerday</a></li> <li><a href="https://github.com/tectalichq/public-openai-client-php">Tectalic/OpenAI</a> by <a href="https://tectalic.com/">tectalic</a></li> </ul> <h2>Python[​](<a href="https://openai.xiniushu.com/docs/libraries#python">https://openai.xiniushu.com/docs/libraries#python</a> &quot;Python的直接链接&quot;)</h2> <ul> <li><a href="https://www.othersideai.com/">OthersideAI</a> by <a href="https://github.com/OthersideAI/chronology">OthersideAI</a></li> </ul> <h2>R[​](<a href="https://openai.xiniushu.com/docs/libraries#r">https://openai.xiniushu.com/docs/libraries#r</a> &quot;R的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/ben-aaron188/rgpt3">RGPT3</a> by <a href="https://github.com/ben-aaron188">Ben-Aaron188</a></li> </ul> <h2>Ruby[​](<a href="https://openai.xiniushu.com/docs/libraries#ruby">https://openai.xiniushu.com/docs/libraries#ruby</a> &quot;Ruby的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/nileshtrivedi">Nileshtrivedi</a> by <a href="https://github.com/nileshtrivedi/openai/">OpenAI</a></li> <li><a href="https://github.com/alexrudall">Alexrudall</a> by <a href="https://github.com/alexrudall/ruby-openai">Ruby-Openai</a></li> </ul> <h2>Scala[​](<a href="https://openai.xiniushu.com/docs/libraries#scala">https://openai.xiniushu.com/docs/libraries#scala</a> &quot;Scala的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/cequence-io/openai-scala-client">OpenAI-Scala-Client</a> by <a href="https://github.com/cequence-io">Cequence-IO</a></li> </ul> <h2>Swift[​](<a href="https://openai.xiniushu.com/docs/libraries#swift">https://openai.xiniushu.com/docs/libraries#swift</a> &quot;Swift的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/dylanshine/openai-kit">OpenAIKit</a> by <a href="https://github.com/dylanshine">dylanshine</a></li> </ul> <h2>Unity[​](<a href="https://openai.xiniushu.com/docs/libraries#unity">https://openai.xiniushu.com/docs/libraries#unity</a> &quot;Unity的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/hexthedev/OpenAi-Api-Unity">OpenAi-Api-Unity</a> by <a href="https://github.com/hexthedev">hexthedev</a></li> </ul> <h2>Unreal Engine[​](<a href="https://openai.xiniushu.com/docs/libraries#unreal-engine">https://openai.xiniushu.com/docs/libraries#unreal-engine</a> &quot;Unreal Engine的直接链接&quot;)</h2> <ul> <li><a href="https://github.com/KellanM/OpenAI-Api-Unreal">OpenAI-API-Unreal</a> by <a href="https://github.com/KellanM">KellanM</a></li> </ul>

页面列表

ITEM_HTML