Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 docs: update ai use docs #257

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions demos/chatgpt-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"start": "next start"
},
"dependencies": {
"@ai-sdk/openai": "^0.0.33",
"@ant-design/pro-chat": "^1.2.0",
"@ant-design/pro-editor": "^0.38.0",
"ai": "^2.2.31",
"@ant-design/pro-editor": "^1.2.1",
"ai": "^3.2.0",
"antd": "^5.12.8",
"antd-style": "^3.6.1",
"next": "14.0.4",
Expand Down
24 changes: 11 additions & 13 deletions demos/chatgpt-nextjs/src/app/api/openai/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import { OpenAIStream, StreamingTextResponse } from 'ai';
import OpenAI from 'openai';
import { createOpenAI } from '@ai-sdk/openai';
import { StreamingTextResponse, streamText } from 'ai';

export async function POST(request: Request) {
const { messages = [] }: Partial<{ messages: Array<any> }> = await request.json();

const openai = new OpenAI({
apiKey: 'OpenAI Key', // your openai key
baseURL: 'base url', // if u dont need change baseUrl,you can delete this line
});

const PickMessages = messages.map((message) => {
return {
role: message.role,
content: message.content,
};
});

const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
const openai = createOpenAI({
// custom settings, e.g.
apiKey: 'OpenAI Key', // your openai key
baseURL: 'base url', // if u dont need change baseUrl,you can delete this line
compatibility: 'compatible',
});
const stream = await streamText({
model: openai('gpt-3.5-turbo'),
messages: [...PickMessages],
stream: true,
});

const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
return new StreamingTextResponse(stream.textStream);
}
30 changes: 14 additions & 16 deletions docs/guide/chatgpt.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,31 @@ We use Vercel's library to parse data streams without the need to manually confi
> We need to combine role and content here because messages contain more content, but for ChatGPT, only these two contents are needed

```ts
import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';
import { createOpenAI } from '@ai-sdk/openai';
import { StreamingTextResponse, streamText } from 'ai';

export const POST = async (request: Request) => {
export async function POST(request: Request) {
const { messages = [] }: Partial<{ messages: Array<any> }> = await request.json();

const openai = new OpenAI({
apiKey: 'OpenAI Key',
baseURL: 'base url',
});

const PickMessages = messages.map((message) => {
return {
role: message.role,
content: message.content,
};
});

const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [...messages],
stream: true,
const openai = createOpenAI({
// custom settings, e.g.
apiKey: 'OpenAI Key', // your openai key
baseURL: 'base url', // if u dont need change baseUrl,you can delete this line
compatibility: 'compatible',
});

const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
};
const stream = await streamText({
model: openai('gpt-3.5-turbo'),
messages: [...PickMessages],
});
return new StreamingTextResponse(stream.textStream);
}
```

## UI
Expand Down
30 changes: 14 additions & 16 deletions docs/guide/chatgpt.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,31 @@ bun add openai
> 这里我们需要将 role 和 content 组合一下,因为 messages 包含的内容会更多一些,但是对于 ChatGPT 来说只需要这两个内容

```ts
import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';
import { createOpenAI } from '@ai-sdk/openai';
import { StreamingTextResponse, streamText } from 'ai';

export const POST = async (request: Request) => {
export async function POST(request: Request) {
const { messages = [] }: Partial<{ messages: Array<any> }> = await request.json();

const openai = new OpenAI({
apiKey: 'OpenAI Key',
baseURL: 'base url',
});

const PickMessages = messages.map((message) => {
return {
role: message.role,
content: message.content,
};
});

const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [...messages],
stream: true,
const openai = createOpenAI({
// custom settings, e.g.
apiKey: 'OpenAI Key', // your openai key
baseURL: 'base url', // if u dont need change baseUrl,you can delete this line
compatibility: 'compatible',
});

const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
};
const stream = await streamText({
model: openai('gpt-3.5-turbo'),
messages: [...PickMessages],
});
return new StreamingTextResponse(stream.textStream);
}
```

## 界面
Expand Down
Loading