Snap openai

前言

随着 Snap! lisp code 变得可用, 搜集一些代码, 挑选, 选择

  • bernat 和 jens 写的代码
  • 示例
  • 课程代码

https://youtu.be/F8n-0kVzEQc?t=2184 puzzle microworld

function call 上的工作,我们可以做到"一切都只是一个Snap!项目"

模仿 Squeak-SemanticText, AI 助手深入了解程序的帮助文档, 语义

demo

downloads snap-openai.xml


create openai client

1
2
3
4
5
6
if (window.openaiClient){return}

url = "https://cdn.jsdelivr.net/npm/@azure/openai@1.0.0-beta.12/+esm" // @latest
import(url).then(openai => {
    window.openaiClient = new openai.OpenAIClient(new openai.OpenAIKeyCredential(apikey));
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai#generate-chatbot-response
async function main(){
  const client = window.openaiClient;
  // https://platform.openai.com/playground/chat 
  const deploymentId = "gpt-3.5-turbo";
  const messages = [
    { role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
    { role: "user", content: "Can you help me?" },
    { role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
    { role: "user", content: "What's the best way to train a parrot?" },
  ];

  console.log(`Messages: ${messages.map((m) => m.content).join("\n")}`);

  const events = await client.streamChatCompletions(deploymentId, messages, { maxTokens: 128 });
  for await (const event of events) {
    for (const choice of event.choices) {
      const delta = choice.delta?.content;
      if (delta !== undefined) {
        console.log(`Chatbot: ${delta}`);
      }
    }
  }
}

main().catch((err) => {
  console.error("The sample encountered an error:", err);
});

参考