This version is preliminary and subject to change
In this lab you will explore how AI chatbots can be used in XState. Additionally, you will be reminded how to use external web services, for instance, to access remote APIs. The lab is composed of the following steps.
- Obtain an ChatGPT API key from Vlad.
- You can work in a new branch of the same repository, branching out from either your Lab 1 code or the starter code.
- Include the function that implements ChatGPT invocation in your code. See the code below. Feel free to modify this function (see 7 below).
- Replace the API key in the invocation function.
- You will need to create a use-case for using ChatGPT in
SpeechState.
- For instance, you can implement an NLU prompt, asking for intents and entities for user input. Hint: you can ask ChatGPT to return structured data (i.e. JSON objects) which you then can parse.
- You need to show that the information that you receive from ChatGPT is processed and further used by your application.
- Feel free to experiment! For instance:
- You can adjust parameters in the API call (such as temperature), see Open AI API docs.
- You can think of different scenarios which make ChatGPT useful, for instance, language generation.
- You can try other models.
- You can use dialogue history (either as part of a single prompt
and or extending the conversation in
messages
). - …and many more!
- Provide a short report (max 1 A4 page) about your experiments.
async function fetchFromChatGPT(prompt: string, max_tokens: number) {
const myHeaders = new Headers();
myHeaders.append(
"Authorization",
"Bearer <your_key_goes_here>",
);
myHeaders.append("Content-Type", "application/json");
const raw = JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: prompt,
},
],
temperature: 0,
max_tokens: max_tokens,
});
const response = fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
})
.then((response) => response.json())
.then((response) => response.choices[0].message.content);
return response;
}
- Invoke
- xstate v5 changes in invoke (I recommend using
fromPromise
)