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

🐛[BUG] 使用 StreamingTextResponse ts 报语法错误 #254

Closed
yuruotong1 opened this issue Jun 25, 2024 · 8 comments · Fixed by #257
Closed

🐛[BUG] 使用 StreamingTextResponse ts 报语法错误 #254

yuruotong1 opened this issue Jun 25, 2024 · 8 comments · Fixed by #257
Labels
documentation Improvements or additions to documentation

Comments

@yuruotong1
Copy link

🐛 bug 描述

我的代码为:
image

image

详细错误信息如下:

Type '(messages: Record<string, any> & ModelConfig) => Promise<StreamingTextResponse>' is not assignable to type 'ChatListRequest<Record<string, any>>'.
  Type 'Promise<StreamingTextResponse>' is not assignable to type 'Promise<ChatMessage<Record<string, any>>[]>'.
    Type 'StreamingTextResponse' is missing the following properties from type 'ChatMessage<Record<string, any>>[]': length, pop, push, concat, and 35 more.ts(2322)
@ONLY-yours ONLY-yours added the documentation Improvements or additions to documentation label Jun 26, 2024
@ONLY-yours
Copy link
Collaborator

应该是这个 ai 包升级导致了用法发生了变化,文档需要更新下了

@yuruotong1
Copy link
Author

yuruotong1 commented Jun 26, 2024

应该是这个 ai 包升级导致了用法发生了变化,文档需要更新下了

请问大佬能不能发一下最新的用法,新版本的ai已经废弃掉了文档的用法!我的openai代码如下:

import { streamText } from "ai";
import { createOpenAI } from '@ai-sdk/openai';

export default async (messages: any)=> {
    const configType =  await window.api.getConfig()
    const llm = JSON.parse(JSON.parse(configType.content).llm)
    // todo从数据库中拿配置项数据
    // const { messages = [] }: Partial<{ messages: Array<any> }> = await request.json();

    const openai = createOpenAI({
    apiKey: llm.apiKey, 
    baseURL: llm.baseURL, 
  });

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

    const res = await streamText({
      model: openai(llm.model),
      messages: messages
  });
    console.log('res', res)
    // const stream = OpenAIStream(res);
    return res.textStream;
}

调用 pro-chat 的代码如下:

 <div style={{ background: theme.colorBgLayout }}>
    <ProChat
        helloMessage={
            '<b>你好,我叫智子,你的智能Agent助手!</b><br><br>你可以输入“/”搜索行为,或者可有什么要求可以随时吩咐!'
        }
        request={async (messages) => {
            console.log('messages', messages.fewShots)
            const response = await useChat(JSON.stringify({ messages: messages }))
            // 使用 Message 作为参数发送请求
            return response// 支持流式和非流式
    }}
  />
  </div>

@ONLY-yours
Copy link
Collaborator

稍后我看看把文档更新一版本,还没来得及看新的用法

@yuruotong1
Copy link
Author

稍后我看看把文档更新一版本,还没来得及看新的用法

感谢您的回复,期待文档更新!

@ONLY-yours ONLY-yours linked a pull request Jun 26, 2024 that will close this issue
@ONLY-yours
Copy link
Collaborator

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 PickMessages = messages.map((message) => {
    return {
      role: message.role,
      content: message.content,
    };
  });

  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],
  });
  return new StreamingTextResponse(stream.textStream);
}

文档在更新中,可以参考这个例子。

@yuruotong1
Copy link
Author

yuruotong1 commented Jun 27, 2024

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 PickMessages = messages.map((message) => {
    return {
      role: message.role,
      content: message.content,
    };
  });

  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],
  });
  return new StreamingTextResponse(stream.textStream);
}

文档在更新中,可以参考这个例子。

@ONLY-yours

我试了一下 pro-chat 组件还是报错,报错信息:

image

我的代码是这么写的:

 <div style={{ background: theme.colorBgLayout }}>
    <ProChat
        helloMessage={
            '你好,我叫智子,你的智能Agent助手!有什么要求可以随时吩咐!'
        }
        request={async (messages) => {
            console.log('messages', JSON.stringify(messages))
            const response = await useChat(JSON.stringify(messages))
            // 使用 Message 作为参数发送请求
            return response// 支持流式和非流式
    }}
  />
  </div>
import { StreamingTextResponse, streamText } from "ai";
import { createOpenAI } from '@ai-sdk/openai';

export async function useChat(messages: any){
  const configType =  await window.api.getConfig()
  const llm = JSON.parse(JSON.parse(configType.content).llm)

  const openai = createOpenAI({
    // custom settings, e.g.
    apiKey: llm.apiKey, // your openai key
    baseURL: llm.baseURL, // if u dont need change baseUrl,you can delete this line
    compatibility: 'compatible',
  });
  const stream = await streamText({
    model: openai(llm.model),
    messages: messages,
  });
  console.log('stream', stream)
  return new StreamingTextResponse(stream.textStream);
}

@ONLY-yours
Copy link
Collaborator

@yuruotong1 我试了下,应该是没问题的,确定下版本:

    "ai": "^3.2.0",
    "openai": "^4.24.7",
    "@ai-sdk/openai": "^0.0.33",

@yuruotong1
Copy link
Author

yuruotong1 commented Jun 27, 2024

@yuruotong1 我试了下,应该是没问题的,确定下版本:

    "ai": "^3.2.0",
    "openai": "^4.24.7",
    "@ai-sdk/openai": "^0.0.33",

@ONLY-yours
我看了你的 demo 代码,你用的 pro-chat 好像不是最新版本?

我用的版本是:

"@ant-design/pro-chat": "^2.1.14",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants