-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3866987
commit ef44eb0
Showing
5 changed files
with
740 additions
and
21 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,116 @@ | ||
import { OpenAI } from "openai"; | ||
import { createAI, getMutableAIState, render } from "ai/rsc"; | ||
import { z } from "zod"; | ||
|
||
interface FlightInfo { | ||
readonly flightNumber: string; | ||
readonly departure: string; | ||
readonly arrival: string; | ||
} | ||
|
||
interface FlightCardProps { | ||
readonly flightInfo: FlightInfo; | ||
} | ||
|
||
type AIStateItem = | ||
| { | ||
readonly role: "user" | "assistant" | "system"; | ||
readonly content: string; | ||
} | ||
| { | ||
readonly role: "function"; | ||
readonly content: string; | ||
readonly name: string; | ||
}; | ||
|
||
interface UIStateItem { | ||
readonly id: number; | ||
readonly display: React.ReactNode; | ||
} | ||
|
||
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); | ||
|
||
async function getFlightInfo(flightNumber: string): Promise<FlightInfo> { | ||
return { | ||
flightNumber, | ||
departure: "New York", | ||
arrival: "San Francisco", | ||
}; | ||
} | ||
|
||
function Spinner() { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
function FlightCard({ flightInfo }: FlightCardProps) { | ||
return ( | ||
<div> | ||
<h2>Flight Information</h2> | ||
<p>Flight Number: {flightInfo.flightNumber}</p> | ||
<p>Departure: {flightInfo.departure}</p> | ||
<p>Arrival: {flightInfo.arrival}</p> | ||
</div> | ||
); | ||
} | ||
|
||
async function submitUserMessage(userInput: string): Promise<UIStateItem> { | ||
"use server"; | ||
|
||
const aiState = getMutableAIState<typeof AI>(); | ||
|
||
aiState.update([...aiState.get(), { role: "user", content: userInput }]); | ||
|
||
const ui = render({ | ||
model: "gpt-4-0125-preview", | ||
provider: openai, | ||
messages: [ | ||
{ role: "system", content: "You are a flight assistant" }, | ||
{ role: "user", content: userInput }, | ||
This comment has been minimized.
Sorry, something went wrong. |
||
...aiState.get(), | ||
], | ||
text: ({ content, done }) => { | ||
if (done) { | ||
aiState.done([...aiState.get(), { role: "assistant", content }]); | ||
} | ||
|
||
return <p>{content}</p>; | ||
}, | ||
tools: { | ||
get_flight_info: { | ||
description: "Get the information for a flight", | ||
parameters: z | ||
.object({ | ||
flightNumber: z.string().describe("the number of the flight"), | ||
}) | ||
.required(), | ||
render: async function* ({ flightNumber }) { | ||
yield <Spinner />; | ||
|
||
const flightInfo = await getFlightInfo(flightNumber); | ||
|
||
aiState.done([ | ||
...aiState.get(), | ||
{ | ||
role: "function", | ||
name: "get_flight_info", | ||
content: JSON.stringify(flightInfo), | ||
}, | ||
]); | ||
|
||
return <FlightCard flightInfo={flightInfo} />; | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return { id: Date.now(), display: ui }; | ||
} | ||
|
||
const initialAIState: AIStateItem[] = []; | ||
const initialUIState: UIStateItem[] = []; | ||
|
||
export const AI = createAI({ | ||
actions: { submitUserMessage }, | ||
initialUIState, | ||
initialAIState, | ||
}); |
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 |
---|---|---|
@@ -1,3 +1,46 @@ | ||
export default function Home() { | ||
return <main>TODO: Add ai/rsc example here.</main>; | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
import { useUIState, useActions } from "ai/rsc"; | ||
import type { AI } from "./action"; | ||
|
||
export default function Page() { | ||
const [inputValue, setInputValue] = useState(""); | ||
const [messages, setMessages] = useUIState<typeof AI>(); | ||
const { submitUserMessage } = useActions<typeof AI>(); | ||
|
||
return ( | ||
<div> | ||
{messages.map((message) => ( | ||
<div key={message.id}>{message.display}</div> | ||
))} | ||
|
||
<form | ||
onSubmit={async (e) => { | ||
e.preventDefault(); | ||
|
||
setMessages((currentMessages) => [ | ||
...currentMessages, | ||
{ id: Date.now(), display: <div>{inputValue}</div> }, | ||
]); | ||
|
||
const responseMessage = await submitUserMessage(inputValue); | ||
setMessages((currentMessages) => [ | ||
...currentMessages, | ||
responseMessage, | ||
]); | ||
|
||
setInputValue(""); | ||
}} | ||
> | ||
<input | ||
placeholder="Send a message..." | ||
value={inputValue} | ||
onChange={(event) => { | ||
setInputValue(event.target.value); | ||
}} | ||
/> | ||
</form> | ||
</div> | ||
); | ||
} |
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.
Whoops, this line can be deleted.