OPENAI浅尝

342 阅读2分钟

简单看了一下openai的官网。尝试了一下3.5的使用指导。 概括来说对于API的使用还是比较容易上手的。 无论是使用TS还是JS接入都没有什么障碍。 尝试了几个简单API的调用

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: "sk-*********************",
});

const openai = new OpenAIApi(configuration);

const completion = openai.createChatCompletion({
  model: "gpt-3.5-turbo",
  messages: [
    {"role": "system", "content": "you are a weapon master!"},
    {"role": "user", "content": "great! they like them. Now they go ahead the forest."},
    {"role": "assistant", "content": "there is a shadow front of them. what you think it is?"},
  ],
});
completion.then((res)=> {
  console.log(res.data.choices[0]);
}, (rej) => {
  console.log(rej);
});

处理结果还是挺像模像样的。

{ message: { role: 'assistant', content: 'For the girl, I would create a lightweight yet powerful bow made of a sturdy wood like yew or hickory, along with a quiver of arrows with razor-sharp arrowheads. The bow and arrows would be ideal for both hunting and self-defense.\n' + '\n' + 'For the boy, I would forge a versatile sword that can be wielded with one or two hands. The blade would be made of high-quality steel for durability and sharpness, with a hilt made of a sturdy material like leather or wood for a comfortable grip.\n' + '\n' + 'As for the old man, I would create a walking staff that doubles as a spear. The staff would be made of a lightweight yet durable wood like ash or maple, with a metal spearhead that can be easily attached and detached as needed.' }, finish_reason: 'stop', index: 0 }

关于官网上的指导,await的写法是错误的。因为createCompletion()的返回值是一个promise,无法直接使用await语法糖。需要包装一个sync函数来使用。或者直接用then来处理。

比较了chatCompletion 和completion。 chatComletion在给定足够的前提下。确实要比completion更给力一些。 但是仍然无法返回更多想象力更丰富的答案。感觉还是被锁住了。并没有让他天马行空。

暂时还没尝试对AI进行调教。不知道会不会有惊喜。

关于图片

const completion = openai.createImage({
  prompt: "you would forge a versatile sword that can be wielded with one or two hands. The blade would be made of high-quality steel for durability and sharpness, with a hilt made of a sturdy material like leather or wood for a comfortable grip.",
  n: 2,
  size: "512x512",
});

image.png

用它自己的描述生成的图片也算有模有样。

这里有一个要注意的地方是。size:只有以下选项。需要注意。填写其他的会报错。不知道是不是我对图片规模不熟悉的原因。

export declare const CreateImageRequestSizeEnum: {
    readonly _256x256: "256x256";
    readonly _512x512: "512x512";
    readonly _1024x1024: "1024x1024";
};

暂时就先了解到这里。以后再继续