ChatGpt 實(shí)在太火爆了,很多人在問(wèn)我怎么使用 chatgpt 開(kāi)發(fā)一個(gè) AI 應(yīng)用程序。這不就來(lái)了嗎~
開(kāi)始
你所需要準(zhǔn)備的一個(gè)OpenAI 的密鑰和一點(diǎn)點(diǎn)代碼來(lái)發(fā)送提示并返回結(jié)果,例如下面這段代碼:
import { OpenAIApi, Configuration } from 'openai'
const openAi = new OpenAIApi(
new Configuration({ apiKey: YOUR_KEY }) // openAi 密鑰
);
async function askGpt(prompt) {
const completion = await openAi.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
});
return completion.data.choices[0].message.content;
}
就靠這段代碼,你現(xiàn)在就可以開(kāi)始用它做出不可思議的東西了!??
構(gòu)造提示模板
使用AI執(zhí)行命令的基本結(jié)構(gòu)是要求用戶輸入,構(gòu)建一個(gè)提示,并生成結(jié)果。
例如,要翻譯文本,你可以使用這樣的代碼:
const text = "Hello, my name is Steve";
const prompt = "Translate to spanish";
const newText = await askGpt(`
I will give you a promnpt to modify some text
The text is: ${text}
The prompt is ${prompt}
Please only return the modified text
`);
// newText is: "Hola, me llamo Steve"
生成代碼
正如你可能已經(jīng)看到的那樣,ChatGPT 不僅可以很好地處理普通單詞,還可以處理代碼。
因此,我們可以輕松構(gòu)建一個(gè)應(yīng)用程序,這個(gè)應(yīng)用是通過(guò)將一個(gè)框架轉(zhuǎn)換為另一個(gè)框架來(lái)幫助你學(xué)習(xí)或遷移到一個(gè)新框架,例如將 React
組件轉(zhuǎn)換為Svelte
。
我們可以制作這樣一個(gè)應(yīng)用:
const sourceFramework = 'react'
const generateFramework = 'svelte'
const sourceCode = `
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const increment = () => { setCount(count + 1) };
const decrement = () => { setCount(count - 1) };
return (
<div>
<h1>Counter: {count}</h1>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
}`
const newCode = await askGpt(`
Please translate this code from ${sourceFramework} to ${generateFramework}.
The code is:
${sourceCode}
Please only give me the new code, no other words.
`)
輸出之后,我們會(huì)得到這樣的 Svelte
代碼:
<script>
let count = 0;
function increment() { count += 1 }
function decrement() { count -= 1 }
</script>
<h1>Counter: {count}</h1>
<button on:click={increment}>Increment</button>
<button on:click={decrement}>Decrement</button>
好了! 是不是非常容易!??!
接下來(lái),我們?cè)賮?lái)看一些例子:
一些例子
AI Shell
將人類語(yǔ)言轉(zhuǎn)換為 CLI 命令,那我可以搞一個(gè)由 AI Shell 來(lái)實(shí)現(xiàn):
// Ask in a CLI
const prompt = 'what is my ip address'
const result = askGpt(`
Please create a one line bash command that can do the following: ${prompt}
`)
將其放入CLI界面中,您將得到:
注意,你甚至不需要知道如何拼寫。??
AI Agents
目前法學(xué)碩士都在活躍地研究領(lǐng)域之一是 AI 代理。這個(gè)應(yīng)用能讓 AI 幫你做一些事情,然后做完這個(gè)事情之后,它會(huì)反饋給你。
const prompt = 'Book a table for 2 for indian food tomorrow'
let actionResult = '';
while (true) {
const result = askGpt(`
You are an AI assistant that can browse the web.
Your prompt is to : ${prompt}
The actions you can take are
- navigate to a URL, like {"action":"navigate","url":"..."}
- click on an element, like {"action":"click", ...}
The result of your last action was: ${actionResult}
What next action will you take? Please just output one action as JSON.
`)
const action = parseAction(result)
actionResult = await executeAction(action)
}
上述代碼正是 GPT 助手——它可以自主瀏覽網(wǎng)頁(yè)以嘗試完成任務(wù)的 AI。
例如,當(dāng)被要求更新 REAMDE 以添加“steve is awesome”時(shí),它向Qwik repo打開(kāi)了一個(gè)pull request:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-429925.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-429925.html
到了這里,關(guān)于來(lái)了來(lái)了,我使用 ChatGPT 開(kāi)發(fā)了一個(gè) AI 應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!