-
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.
For now I am just pushing in the state where I was with my fork of @Zakinator123's fork. I haven't updated to all the latest comments of that branch, but I wanted to get this available for those that were looking for the Svelte version, if they wanted to clone off this branch to work locally. I'll upate to include the rest of the changes requested in the other PR here as I can.
- Loading branch information
1 parent
51cb3ee
commit 6648b21
Showing
7 changed files
with
552 additions
and
279 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 | ||
--- | ||
|
||
Add experimental client side OpenAI function calling to Svelte bindings |
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 @@ | ||
OPENAI_API_KEY=xxxxxxx |
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
73 changes: 73 additions & 0 deletions
73
examples/sveltekit-openai/src/routes/api/chat-with-functions/+server.ts
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,73 @@ | ||
import { Configuration, OpenAIApi } from 'openai-edge' | ||
import { OpenAIStream, StreamingTextResponse } from 'ai' | ||
import type { ChatCompletionFunctions } from 'openai-edge/types/api' | ||
|
||
import { env } from '$env/dynamic/private' | ||
|
||
const config = new Configuration({ | ||
apiKey: env.OPENAI_API_KEY | ||
}) | ||
const openai = new OpenAIApi(config) | ||
|
||
const functions: ChatCompletionFunctions[] = [ | ||
{ | ||
name: 'get_current_weather', | ||
description: 'Get the current weather', | ||
parameters: { | ||
type: 'object', | ||
properties: { | ||
location: { | ||
type: 'string', | ||
description: 'The city and state, e.g. San Francisco, CA' | ||
}, | ||
format: { | ||
type: 'string', | ||
enum: ['celsius', 'fahrenheit'], | ||
description: | ||
'The temperature unit to use. Infer this from the users location.' | ||
} | ||
}, | ||
required: ['location', 'format'] | ||
} | ||
}, | ||
{ | ||
name: 'get_current_time', | ||
description: 'Get the current time', | ||
parameters: { | ||
type: 'object', | ||
properties: {}, | ||
required: [] | ||
} | ||
}, | ||
{ | ||
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 async function POST({ request }) { | ||
const { messages, function_call } = await request.json() | ||
|
||
const response = await openai.createChatCompletion({ | ||
model: 'gpt-3.5-turbo-0613', | ||
stream: true, | ||
messages, | ||
functions, | ||
function_call | ||
}) | ||
|
||
const stream = OpenAIStream(response) | ||
return new StreamingTextResponse(stream) | ||
} |
128 changes: 128 additions & 0 deletions
128
examples/sveltekit-openai/src/routes/chat-with-functions/+page.svelte
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,128 @@ | ||
<script lang="ts"> | ||
import { useChat } from 'ai/svelte' | ||
import type { ChatRequest, FunctionCallHandler } from 'ai' | ||
import { nanoid } from 'ai' | ||
const functionCallHandler: FunctionCallHandler = async ( | ||
chatMessages, | ||
functionCall | ||
) => { | ||
if (functionCall.name === 'get_current_weather') { | ||
if (functionCall.arguments) { | ||
const parsedFunctionCallArguments = JSON.parse(functionCall.arguments) | ||
// You now have access to the parsed arguments here (assuming the JSON was valid) | ||
// If JSON is invalid, return an appropriate message to the model so that it may retry? | ||
console.log(parsedFunctionCallArguments) | ||
} | ||
// Generate a fake temperature | ||
const temperature = Math.floor(Math.random() * (100 - 30 + 1) + 30) | ||
// Generate random weather condition | ||
const weather = ['sunny', 'cloudy', 'rainy', 'snowy'][ | ||
Math.floor(Math.random() * 4) | ||
] | ||
const functionResponse: ChatRequest = { | ||
messages: [ | ||
...chatMessages, | ||
{ | ||
id: nanoid(), | ||
name: 'get_current_weather', | ||
role: 'function' as const, | ||
content: JSON.stringify({ | ||
temperature, | ||
weather, | ||
info: 'This data is randomly generated and came from a fake weather API!' | ||
}) | ||
} | ||
] | ||
} | ||
return functionResponse | ||
} else if (functionCall.name === 'get_current_time') { | ||
const time = new Date().toLocaleTimeString() | ||
const functionResponse: ChatRequest = { | ||
messages: [ | ||
...chatMessages, | ||
{ | ||
id: nanoid(), | ||
name: 'get_current_time', | ||
role: 'function' as const, | ||
content: JSON.stringify({ time }) | ||
} | ||
] | ||
// You can also (optionally) return a list of functions here that the model can call next | ||
// functions | ||
} | ||
return functionResponse | ||
} else 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 | ||
) | ||
const functionResponse = { | ||
messages: [ | ||
...chatMessages, | ||
{ | ||
id: nanoid(), | ||
name: 'eval_code_in_browser', | ||
role: 'function' as const, | ||
content: JSON.stringify(eval(parsedFunctionCallArguments.code)) | ||
} | ||
] | ||
} | ||
return functionResponse | ||
} | ||
} | ||
} | ||
const { messages, input, handleSubmit } = useChat({ | ||
api: '/api/chat-with-functions', | ||
experimental_onFunctionCall: functionCallHandler | ||
}) | ||
</script> | ||
|
||
<svelte:head> | ||
<title>Home</title> | ||
<meta name="description" content="Svelte demo app" /> | ||
</svelte:head> | ||
|
||
<section> | ||
<h1>useChat</h1> | ||
|
||
<p> | ||
This is a demo of the <code>useChat</code> hook. It uses the | ||
<code>experimental_onFunctionCall</code> option to handle function calls from | ||
the model. | ||
</p> | ||
<p> | ||
The available functions are: <code>get_current_weather</code>, | ||
<code>get_current_time</code> | ||
and <code>eval_code_in_browser</code>. | ||
</p> | ||
|
||
<ul> | ||
{#each $messages as message} | ||
<li>{message.role}: {message.content}</li> | ||
{/each} | ||
</ul> | ||
<form on:submit={handleSubmit}> | ||
<input bind:value={$input} /> | ||
<button type="submit">Send</button> | ||
</form> | ||
</section> | ||
|
||
<style> | ||
section { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
flex: 0.6; | ||
} | ||
h1 { | ||
width: 100%; | ||
} | ||
</style> |
Oops, something went wrong.