-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vue: Add complex response parsing and StreamData support to useChat (#…
…762)
- Loading branch information
Showing
8 changed files
with
271 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
ai/vue: Add complex response parsing and StreamData support to useChat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<script setup lang="ts"> | ||
import { useChat } from 'ai/vue'; | ||
import { nanoid } from 'ai'; | ||
import type { FunctionCallHandler, Message } from 'ai'; | ||
const functionCallHandler: FunctionCallHandler = async ( | ||
chatMessages, | ||
functionCall, | ||
) => { | ||
if (functionCall.name === 'eval_code_in_browser') { | ||
if (functionCall.arguments) { | ||
// Parsing here does not always work since it seems that some characters in generated code aren't escaped properly. | ||
const parsedFunctionCallArguments: { code: string } = JSON.parse( | ||
functionCall.arguments, | ||
); | ||
// WARNING: Do NOT do this in real-world applications! | ||
eval(parsedFunctionCallArguments.code); | ||
const functionResponse = { | ||
messages: [ | ||
...chatMessages, | ||
{ | ||
id: nanoid(), | ||
name: 'eval_code_in_browser', | ||
role: 'function' as const, | ||
content: parsedFunctionCallArguments.code, | ||
}, | ||
], | ||
}; | ||
return functionResponse; | ||
} | ||
} | ||
}; | ||
const { messages, input, handleSubmit } = useChat({ | ||
api: '/api/chat-with-functions', | ||
experimental_onFunctionCall: functionCallHandler, | ||
}); | ||
// Generate a map of message role to text color | ||
const roleToColorMap: Record<Message['role'], string> = { | ||
system: 'red', | ||
user: 'black', | ||
function: 'blue', | ||
assistant: 'green', | ||
}; | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-col w-full max-w-md py-24 mx-auto stretch"> | ||
<div | ||
v-for="m in messages" | ||
key="m.id" | ||
class="whitespace-pre-wrap" | ||
:style="{ color: roleToColorMap[m.role] }" | ||
> | ||
<strong>{{ m.role }}:</strong> | ||
{{ m.content || JSON.stringify(m.function_call) }} | ||
<br /> | ||
<br /> | ||
</div> | ||
|
||
<form @submit="handleSubmit"> | ||
<input | ||
class="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl" | ||
v-model="input" | ||
placeholder="Say something..." | ||
/> | ||
</form> | ||
</div> | ||
</template> |
6 changes: 2 additions & 4 deletions
6
examples/nuxt-openai/app.vue → examples/nuxt-openai/pages/index.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
OpenAIStream, | ||
StreamingTextResponse, | ||
experimental_StreamData, | ||
} from 'ai'; | ||
import OpenAI from 'openai'; | ||
import type { ChatCompletionCreateParams } from 'openai/resources/chat'; | ||
|
||
const functions: ChatCompletionCreateParams.Function[] = [ | ||
{ | ||
name: 'get_current_weather', | ||
description: 'Get the current weather.', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
format: { | ||
type: 'string', | ||
enum: ['celsius', 'fahrenheit'], | ||
description: 'The temperature unit to use.', | ||
}, | ||
}, | ||
required: ['format'], | ||
}, | ||
}, | ||
{ | ||
name: 'eval_code_in_browser', | ||
description: 'Execute javascript code in the browser with eval().', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
code: { | ||
type: 'string', | ||
description: `Javascript code that will be directly executed via eval(). Do not use backticks in your response. | ||
DO NOT include any newlines in your response, and be sure to provide only valid JSON when providing the arguments object. | ||
The output of the eval() will be returned directly by the function.`, | ||
}, | ||
}, | ||
required: ['code'], | ||
}, | ||
}, | ||
]; | ||
|
||
export default defineLazyEventHandler(async () => { | ||
const apiKey = useRuntimeConfig().openaiApiKey; | ||
if (!apiKey) throw new Error('Missing OpenAI API key'); | ||
const openai = new OpenAI({ | ||
apiKey: apiKey, | ||
}); | ||
|
||
return defineEventHandler(async (event: any) => { | ||
const { messages } = await readBody(event); | ||
|
||
const response = await openai.chat.completions.create({ | ||
model: 'gpt-3.5-turbo-0613', | ||
stream: true, | ||
messages, | ||
functions, | ||
}); | ||
|
||
const data = new experimental_StreamData(); | ||
const stream = OpenAIStream(response, { | ||
experimental_onFunctionCall: async ( | ||
{ name, arguments: args }, | ||
createFunctionCallMessages, | ||
) => { | ||
if (name === 'get_current_weather') { | ||
// Call a weather API here | ||
const weatherData = { | ||
temperature: 20, | ||
unit: args.format === 'celsius' ? 'C' : 'F', | ||
}; | ||
|
||
data.append({ | ||
text: 'Some custom data', | ||
}); | ||
|
||
const newMessages = createFunctionCallMessages(weatherData); | ||
return openai.chat.completions.create({ | ||
messages: [...messages, ...newMessages], | ||
stream: true, | ||
model: 'gpt-3.5-turbo-0613', | ||
}); | ||
} | ||
}, | ||
onCompletion(completion) { | ||
console.log('completion', completion); | ||
}, | ||
onFinal(completion) { | ||
data.close(); | ||
}, | ||
experimental_streamData: true, | ||
}); | ||
|
||
data.append({ | ||
text: 'Hello, how are you?', | ||
}); | ||
|
||
return new StreamingTextResponse(stream, {}, data); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.