Giter Club home page Giter Club logo

Comments (20)

JoseAngelChepo avatar JoseAngelChepo commented on June 10, 2024 4

The latest update "streamUI instead of render #324" does not allow messages with "role: system" between conversation (only at start) with new prompt param system.

The "problem" is:
The functions also uses messages with "role: system" to register the actions or events from the user in the context conversation.

The error Cannot destructure property 'role' error is consequence of the function below that return undefined for "role: system"

This function is inside of dependency "ai": "^3.1.1"

// core/prompt/convert-to-language-model-prompt.ts
function convertToLanguageModelPrompt(prompt) {
  const languageModelMessages = [];
  if (prompt.system != null) {
    languageModelMessages.push({ role: "system", content: prompt.system });
  }
  switch (prompt.type) {
    case "prompt": {
      languageModelMessages.push({
        role: "user",
        content: [{ type: "text", text: prompt.prompt }]
      });
      break;
    }
    case "messages": {
      languageModelMessages.push(
        ...prompt.messages.map((message) => {
          switch (message.role) {
            case "user": {
              if (typeof message.content === "string") {
                return {
                  role: "user",
                  content: [{ type: "text", text: message.content }]
                };
              }
              return {
                role: "user",
                content: message.content.map(
                  (part) => {
                    var _a;
                    switch (part.type) {
                      case "text": {
                        return part;
                      }
                      case "image": {
                        if (part.image instanceof URL) {
                          return {
                            type: "image",
                            image: part.image,
                            mimeType: part.mimeType
                          };
                        }
                        const imageUint8 = convertDataContentToUint8Array(
                          part.image
                        );
                        return {
                          type: "image",
                          image: imageUint8,
                          mimeType: (_a = part.mimeType) != null ? _a : detectImageMimeType(imageUint8)
                        };
                      }
                    }
                  }
                )
              };
            }
            case "assistant": {
              if (typeof message.content === "string") {
                return {
                  role: "assistant",
                  content: [{ type: "text", text: message.content }]
                };
              }
              return { role: "assistant", content: message.content };
            }
            case "tool": {
              return message;
            }
          }
        })
      );
      break;
    }
    default: {
      const _exhaustiveCheck = prompt;
      throw new Error(`Unsupported prompt type: ${_exhaustiveCheck}`);
    }
  }
  return languageModelMessages;
}

I tested in compiled module inside node_modules/ai/rsc/dist/rsc-server.msj function convertToLanguageModelPrompt by adding

case "system": {
    return message;
}

Result:
Captura de pantalla 2024-05-06 a la(s) 2 01 11 p m

This works but the correction must be made in the repository of the dependency "ai": "^3.1.1".

And this depends on whether it is considered good practice to send system messages inside of the conversation to save new context (user events)

from ai-chatbot.

prashantbhudwal avatar prashantbhudwal commented on June 10, 2024 3

@jeremyphilemon @jaredpalmer

the messages sent to openai are not formatted properly.

Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.

I was testing. This works.

const toolCallMessage: CoreMessage = {
  role: "assistant",
  content: [
    {
      type: "tool-call",
      toolName: "showStockPurchase",
      toolCallId: "8Bb6oJ1vAIRuHSIAVYmAp",
      args: z.object({
        symbol: z.string(),
        price: z.number(),
        defaultAmount: z.number(),
      }),
    },
  ],
};

const toolMessage: CoreMessage = {
  role: "tool",
  content: [
    {
      result: JSON.stringify({
        symbol: "AAPL",
        price: 150,
        defaultAmount: 100,
        status: "completed",
      }),
      type: "tool-result",
      toolCallId: "8Bb6oJ1vAIRuHSIAVYmAp",
      toolName: "showStockPurchase",
    },
  ],
};

  const all = [...messages, toolCallMessage, toolMessage];

@athrael-soju
This is not a solution, just a problem description. I am still learning this. So, I don't think I am the best person for the PR.

from ai-chatbot.

athrael-soju avatar athrael-soju commented on June 10, 2024 3

Because the new SDK uses streamUI

const result = await streamUI({

And old one uses a render

const ui = render({

The bug appears when using streamUI, not render. If you revert to the previous commit it works fine with render.

from ai-chatbot.

prashantbhudwal avatar prashantbhudwal commented on June 10, 2024 2

@jeremyphilemon @jaredpalmer

the messages sent to openai are not formatted properly.

Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.

I was testing. This works.

const toolCallMessage: CoreMessage = {
  role: "assistant",
  content: [
    {
      type: "tool-call",
      toolName: "showStockPurchase",
      toolCallId: "8Bb6oJ1vAIRuHSIAVYmAp",
      args: z.object({
        symbol: z.string(),
        price: z.number(),
        defaultAmount: z.number(),
      }),
    },
  ],
};

const toolMessage: CoreMessage = {
  role: "tool",
  content: [
    {
      result: JSON.stringify({
        symbol: "AAPL",
        price: 150,
        defaultAmount: 100,
        status: "completed",
      }),
      type: "tool-result",
      toolCallId: "8Bb6oJ1vAIRuHSIAVYmAp",
      toolName: "showStockPurchase",
    },
  ],
};

  const all = [...messages, toolCallMessage, toolMessage];

from ai-chatbot.

hemik000 avatar hemik000 commented on June 10, 2024 1

Same error. Any solution?

from ai-chatbot.

athrael-soju avatar athrael-soju commented on June 10, 2024

@prashantbhudwal Could you raise a PR please?

from ai-chatbot.

Spectralgo avatar Spectralgo commented on June 10, 2024

Same kind of error here with this error message: Cannot destructure property 'role' of '.for' as it is undefined. at convertToOpenAIChatMessage

Can't have the app reply after showing me a card.

image

Time for me to study some docs 📃 and try some implementations !! GLHF

UPDATE:

I've tried to check out to the previous commit and the bug disapears:
image

from ai-chatbot.

hemik000 avatar hemik000 commented on June 10, 2024

Because the new SDK uses streamUI

const result = await streamUI({

And old one uses a render

const ui = render({

from ai-chatbot.

DanielhCarranza avatar DanielhCarranza commented on June 10, 2024

I have the same error after clicking on the first generated component, I update everything even nextjs but it didn't work

from ai-chatbot.

fullstackwebdev avatar fullstackwebdev commented on June 10, 2024

Yes, this is an issue with the new code. I guess it should be reverted or fixed? I tried a few things to resolve it, but wasn't able to.

from ai-chatbot.

kevb10 avatar kevb10 commented on June 10, 2024

The above didn't solve my problem. There is a phantom object that is undefined on my end. And that's causing the error
image

Obviously a little sanity check addresses this but I'm curious where and why an operation returns undefined in the first place and fix it there instead.
image

from ai-chatbot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.