Skip to content

Commit f42bbb5

Browse files
authoredApr 9, 2024··
Remove experimental from useAssistant and AssistantResponse. (#1306)
1 parent f6f227d commit f42bbb5

File tree

9 files changed

+78
-35
lines changed

9 files changed

+78
-35
lines changed
 

Diff for: ‎.changeset/gentle-poets-check.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
Remove experimental from useAssistant and AssistantResponse.

Diff for: ‎docs/pages/docs/api-reference/_meta.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"generative-ui": "Generative UI",
33
"providers": "Providers",
4-
"use-assistant": "experimental_useAssistant",
4+
"use-assistant": "useAssistant",
55
"use-chat": "useChat",
66
"use-completion": "useCompletion",
77
"ai-stream": "AIStream",

Diff for: ‎docs/pages/docs/api-reference/providers/_meta.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"mistral-stream": "MistralStream",
1010
"replicate-stream": "ReplicateStream",
1111
"inkeep-stream": "InkeepStream",
12-
"assistant-response": "experimental_AssistantResponse"
12+
"assistant-response": "AssistantResponse"
1313
}

Diff for: ‎docs/pages/docs/api-reference/providers/assistant-response.mdx

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
---
2-
title: experimental_AssistantResponse
2+
title: AssistantResponse
33
layout:
44
toc: false
55
---
66

77
import { Callout } from 'nextra-theme-docs';
88

9-
# `experimental_AssistantResponse`
9+
# `AssistantResponse`
1010

11-
The `experimental_AssistantResponse` class allows you to send a stream of assistant update to `experimental_useAssistant`.
11+
The `AssistantResponse` allows you to send a stream of assistant update to `useAssistant`.
1212

13-
<Callout>
14-
The `experimental_` prefix indicates that the API is not yet stable and may
15-
change in the future without a major version bump.
16-
</Callout>
13+
## `AssistantResponse(settings: AssistantResponseSettings, process: AssistantResponseCallback): Response` [#assistantresponse]
1714

18-
## `experimental_AssistantResponse(settings: AssistantResponseSettings, process: AssistantResponseCallback): Response` [#assistantresponse]
19-
20-
The `experimental_AssistantResponse` class is designed to facilitate streaming assistant responses to the `useAssistant` hook.
15+
The `AssistantResponse` is designed to facilitate streaming assistant responses to the `useAssistant` hook.
2116
It receives an assistant thread and a current message, and can send messages and data messages to the client.
2217

2318
## Parameters
2419

2520
### `settings: {threadId: string, messageId: string}`
2621

27-
You can pass the thread and the latest message into the `experimental_AssistantResponse`. This establishes the context for the response.
22+
You can pass the thread and the latest message into the `AssistantResponse`. This establishes the context for the response.
2823

2924
- `threadId: string`: The thread ID that the response is associated with.
3025
- `messageId: string`: The ID of the latest message that the response is associated with.
@@ -42,13 +37,13 @@ It gets invoked with the following functions that you can use to send messages a
4237

4338
### Server-Side Implementation
4439

45-
This example highlights the usage of `experimental_AssistantResponse`
40+
This example highlights the usage of `AssistantResponse`
4641
for an OpenAI assistant within a Next.js environment.
4742

4843
Server:
4944

5045
```tsx filename="app/api/assistant/route.ts"
51-
import { experimental_AssistantResponse } from 'ai';
46+
import { AssistantResponse } from 'ai';
5247
import OpenAI from 'openai';
5348

5449
// Create an OpenAI API client (that's edge friendly!)
@@ -83,7 +78,7 @@ export async function POST(req: Request) {
8378
content: input.message,
8479
});
8580

86-
return experimental_AssistantResponse(
81+
return AssistantResponse(
8782
{ threadId, messageId: createdMessage.id },
8883
async ({ forwardStream, sendDataMessage }) => {
8984
// Run the assistant on the thread

Diff for: ‎docs/pages/docs/api-reference/use-assistant.mdx

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
2-
title: experimental_useAssistant
2+
title: useAssistant
33
---
44

55
import { OptionTable } from '@/components/table';
66
import { FrameworkTabs, Tab } from '@/components/framework-tabs';
77

8-
# experimental_useAssistant
8+
# useAssistant
99

10-
## `experimental_useAssistant(options: UseAssistantOptions): UseAssistantHelpers` [#experimental_useAssistant]
10+
## `useAssistant(options: UseAssistantOptions): UseAssistantHelpers` [#useAssistant]
1111

1212
`useAssistant` is a utility designed to handle the UI state of interacting with an OpenAI-compatible assistant API.
1313
This tool is useful when you need to integrate assistant capabilities into your application,
@@ -16,17 +16,13 @@ It works in conjunction with `AssistantResponse` in the backend.
1616

1717
<FrameworkTabs>
1818
<Tab>
19-
To use `experimental_useAssistant` in React projects, you can import it from the `ai/react` subpath.
20-
Here's an example demonstrating the use of `experimental_useAssistant` in a chat interface:
19+
To use `useAssistant` in React projects, you can import it from the `ai/react` subpath.
20+
Here's an example demonstrating the use of `useAssistant` in a chat interface:
2121

2222
```tsx filename="app/assistant.tsx"
2323
'use client';
2424

25-
import {
26-
Message,
27-
// import as useAssistant:
28-
experimental_useAssistant as useAssistant,
29-
} from 'ai/react';
25+
import { Message, useAssistant } from 'ai/react';
3026

3127
const roleToColorMap: Record<Message['role'], string> = {
3228
system: 'red',
@@ -124,7 +120,7 @@ export default function Chat() {
124120

125121
### `UseAssistantHelpers`
126122

127-
The `experimental_useAssistant` hook returns an object with several helper methods and variables to manage the assistant state in the UI:
123+
The `useAssistant` hook returns an object with several helper methods and variables to manage the assistant state in the UI:
128124

129125
<OptionTable
130126
options={[

Diff for: ‎examples/next-openai/app/api/assistant/route.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { experimental_AssistantResponse } from 'ai';
1+
import { AssistantResponse } from 'ai';
22
import OpenAI from 'openai';
33

44
// Create an OpenAI API client (that's edge friendly!)
@@ -33,7 +33,7 @@ export async function POST(req: Request) {
3333
content: input.message,
3434
});
3535

36-
return experimental_AssistantResponse(
36+
return AssistantResponse(
3737
{ threadId, messageId: createdMessage.id },
3838
async ({ forwardStream, sendDataMessage }) => {
3939
// Run the assistant on the thread

Diff for: ‎examples/next-openai/app/assistant/page.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { Message, experimental_useAssistant as useAssistant } from 'ai/react';
3+
import { Message, useAssistant as useAssistant } from 'ai/react';
44
import { useEffect, useRef } from 'react';
55

66
const roleToColorMap: Record<Message['role'], string> = {
@@ -29,7 +29,7 @@ export default function Chat() {
2929
return (
3030
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
3131
{error != null && (
32-
<div className="relative bg-red-500 text-white px-6 py-4 rounded-md">
32+
<div className="relative px-6 py-4 text-white bg-red-500 rounded-md">
3333
<span className="block sm:inline">
3434
Error: {(error as any).toString()}
3535
</span>
@@ -59,7 +59,7 @@ export default function Chat() {
5959
))}
6060

6161
{status === 'in_progress' && (
62-
<div className="h-8 w-full max-w-md p-2 mb-8 bg-gray-300 dark:bg-gray-600 rounded-lg animate-pulse" />
62+
<div className="w-full h-8 max-w-md p-2 mb-8 bg-gray-300 rounded-lg dark:bg-gray-600 animate-pulse" />
6363
)}
6464

6565
<form onSubmit={submitMessage}>

Diff for: ‎packages/core/react/use-assistant.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export type UseAssistantOptions = {
100100
onError?: (error: Error) => void;
101101
};
102102

103-
export function experimental_useAssistant({
103+
export function useAssistant({
104104
api,
105105
threadId: threadIdParam,
106106
credentials,
@@ -252,3 +252,8 @@ export function experimental_useAssistant({
252252
error,
253253
};
254254
}
255+
256+
/**
257+
@deprecated Use `useAssistant` instead.
258+
*/
259+
export const experimental_useAssistant = useAssistant;

Diff for: ‎packages/core/streams/assistant-response.ts

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,59 @@
11
import { AssistantStream } from 'openai/lib/AssistantStream';
2+
import { Run } from 'openai/resources/beta/threads/runs/runs';
23
import { formatStreamPart } from '../shared/stream-parts';
34
import { AssistantMessage, DataMessage } from '../shared/types';
4-
import { Run } from 'openai/resources/beta/threads/runs/runs';
55

6+
/**
7+
You can pass the thread and the latest message into the `AssistantResponse`. This establishes the context for the response.
8+
*/
69
type AssistantResponseSettings = {
10+
/**
11+
The thread ID that the response is associated with.
12+
*/
713
threadId: string;
14+
15+
/**
16+
The ID of the latest message that the response is associated with.
17+
*/
818
messageId: string;
919
};
1020

21+
/**
22+
The process parameter is a callback in which you can run the assistant on threads, and send messages and data messages to the client.
23+
*/
1124
type AssistantResponseCallback = (options: {
25+
/**
26+
@deprecated use variable from outer scope instead.
27+
*/
1228
threadId: string;
29+
30+
/**
31+
@deprecated use variable from outer scope instead.
32+
*/
1333
messageId: string;
34+
35+
/**
36+
Forwards an assistant message (non-streaming) to the client.
37+
*/
1438
sendMessage: (message: AssistantMessage) => void;
39+
40+
/**
41+
Send a data message to the client. You can use this to provide information for rendering custom UIs while the assistant is processing the thread.
42+
*/
1543
sendDataMessage: (message: DataMessage) => void;
44+
45+
/**
46+
Forwards the assistant response stream to the client. Returns the `Run` object after it completes, or when it requires an action.
47+
*/
1648
forwardStream: (stream: AssistantStream) => Promise<Run | undefined>;
1749
}) => Promise<void>;
1850

19-
export function experimental_AssistantResponse(
51+
/**
52+
The `AssistantResponse` allows you to send a stream of assistant update to `useAssistant`.
53+
It is designed to facilitate streaming assistant responses to the `useAssistant` hook.
54+
It receives an assistant thread and a current message, and can send messages and data messages to the client.
55+
*/
56+
export function AssistantResponse(
2057
{ threadId, messageId }: AssistantResponseSettings,
2158
process: AssistantResponseCallback,
2259
): Response {
@@ -120,3 +157,8 @@ export function experimental_AssistantResponse(
120157
},
121158
});
122159
}
160+
161+
/**
162+
@deprecated Use `AssistantResponse` instead.
163+
*/
164+
export const experimental_AssistantResponse = AssistantResponse;

0 commit comments

Comments
 (0)
Please sign in to comment.