@@ -58,17 +58,7 @@ Add `AI_GATEWAY_API_KEY` to your environment:
5858AI_GATEWAY_API_KEY=your_api_key_here
5959```
6060
61- Then use it with the AI SDK:
62-
63- ``` ts title="app/api/chat/route.ts"
64- import { createOpenAI } from " @ai-sdk/openai" ;
65-
66- const openai = createOpenAI ({
67- apiKey: process .env .AI_GATEWAY_API_KEY ,
68- baseURL:
69- " https://gateway.ai.cloudflare.com/v1/your_account_id_here/ai-gateway/openai" ,
70- });
71- ```
61+ Then use it with the AI SDK by specifying a model string e.g. "openai/gpt-4o"
7262
7363## AI SDK
7464
@@ -87,15 +77,25 @@ The [AI SDK](https://ai-sdk.dev/) provides the foundation for AI interactions.
8777import { useChat } from " @ai-sdk/react" ;
8878
8979function Chat() {
90- const { messages, input, handleInputChange, handleSubmit, status } =
91- useChat ();
80+ const [text, setText] = useState (" " );
81+ const { messages, sendMessage, status } = useChat ({
82+ transport: new DefaultChatTransport ({
83+ api: " /api/weather" ,
84+ }),
85+ });
86+
87+ const handleSubmit = (e : React .FormEvent ) => {
88+ e .preventDefault ();
89+ sendMessage ({ text: text });
90+ setText (" " );
91+ };
9292
9393 return (
9494 <form onSubmit = { handleSubmit } >
9595 { messages .map ((m ) => (
9696 <div key = { m .id } >{ m .content } </div >
9797 ))}
98- <input value = { input } onChange = { handleInputChange } />
98+ <input value = { text } onChange = { ( e ) => setText ( e . target . value ) } />
9999 </form >
100100 );
101101}
@@ -105,17 +105,16 @@ function Chat() {
105105
106106``` ts title="app/api/chat/route.ts"
107107import { streamText } from " ai" ;
108- import { openai } from " @ai-sdk/openai" ;
109108
110109export async function POST(req : Request ) {
111110 const { messages } = await req .json ();
112111
113112 const result = streamText ({
114- model: openai ( " gpt-4o" ) ,
113+ model: " openai/ gpt-4o" ,
115114 messages ,
116115 });
117116
118- return result .toDataStreamResponse ();
117+ return result .toUIMessageStreamResponse ();
119118}
120119```
121120
@@ -152,8 +151,18 @@ import {
152151} from " @/components/ai-elements/prompt-input" ;
153152
154153export default function ChatPage() {
155- const { messages, input, handleInputChange, handleSubmit, status } =
156- useChat ();
154+ const [text, setText] = useState (" " );
155+ const { messages, sendMessage, status } = useChat ({
156+ transport: new DefaultChatTransport ({
157+ api: " /api/weather" ,
158+ }),
159+ });
160+
161+ const handleSubmit = (e : React .FormEvent ) => {
162+ e .preventDefault ();
163+ sendMessage ({ text });
164+ setText (" " );
165+ };
157166
158167 return (
159168 <div className = " h-screen flex flex-col" >
@@ -165,7 +174,7 @@ export default function ChatPage() {
165174 { message .parts .map ((part , i ) =>
166175 part .type === " text" ? (
167176 <MessageResponse key = { i } >{ part .text } </MessageResponse >
168- ) : null
177+ ) : null ,
169178 )}
170179 </MessageContent >
171180 </Message >
@@ -175,8 +184,8 @@ export default function ChatPage() {
175184
176185 <Input onSubmit = { handleSubmit } className = " p-4" >
177186 <PromptInputTextarea
178- value = { input }
179- onChange = { handleInputChange }
187+ value = { text }
188+ onChange = { ( e ) => setText ( e . target . value ) }
180189 placeholder = " Type a message..."
181190 />
182191 <PromptInputSubmit
0 commit comments