langchainjs: Getting started code for chatModel throws error

Following the code here.

The output is:

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'message')
TypeError: Cannot read properties of undefined (reading 'message')
    at base.js:185:34

Looking at “base.js”

    /**
     * Makes a single call to the chat model.
     * @param messages An array of BaseMessage instances.
     * @param options The call options or an array of stop sequences.
     * @param callbacks The callbacks for the language model.
     * @returns A Promise that resolves to a BaseMessage.
     */
    async call(messages, options, callbacks) {
        const result = await this.generate([messages.map(coerceMessageLikeToMessage)], options, callbacks);
        const generations = result.generations;
        return generations[0][0].message;
    }

Not sure how to proceed as this is failing after adding the code from the docs.

Respone from Network looks good

{
  "id": "chatcmpl-8FkXETihLHC5MAjReDMqY7rNR2ddh",
  "object": "chat.completion",
  "created": 1698764492,
  "model": "gpt-4-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Rainbow Steps Socks Co."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 22,
    "completion_tokens": 7,
    "total_tokens": 29
  }
}

package.json

    "langchain": "^0.0.150",

For reference original code:

import { Component } from '@angular/core';
import { environment } from '../environments/environment.development';
import { ChatOpenAI } from 'langchain/chat_models/openai';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title = 'chatbot';

  async call() {
    const chatModel = new ChatOpenAI({
      modelName: "gpt-4",
      temperature: 0.9,
      openAIApiKey: environment.OPENAI_API_KEY,
    });
    const text = 'What would be a good company name for a company that makes colorful socks?';
    const chatModelResult = await chatModel.predict(text);
    console.log(chatModelResult);
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Comments: 17

Most upvoted comments

🤖

Hello @gsans,

I’m here to assist you with your issue. It seems like you’re encountering a TypeError when trying to implement the getting started code for chatModel from the LangChainJS documentation.

The error you’re seeing typically occurs when you’re trying to access a property of an object that hasn’t been defined. In your case, it seems like the ‘message’ property of the ‘generations[0][0]’ object is undefined.

Looking at your network response, it seems like the response structure is slightly different from what the call method in base.js expects. The call method tries to access the ‘message’ property of the first item in the ‘generations’ array. However, in your network response, the ‘message’ property is nested inside the ‘choices’ array, which is not being accessed in the call method.

Here’s the relevant part of your network response:

"choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Rainbow Steps Socks Co."
      },
      "finish_reason": "stop"
    }
]

And here’s the relevant part of the call method:

const generations = result.generations;
return generations[0][0].message;

As you can see, the call method is trying to access ‘message’ directly from the first item in the ‘generations’ array, but in your network response, ‘message’ is nested inside the ‘choices’ array.

To fix this issue, you might need to adjust the call method to correctly access the ‘message’ property from the network response. Here’s an example of how you could do this:

const generations = result.generations;
return generations[0][0].choices[0].message;

Please note that this is just a suggestion and might not work depending on the exact structure of your ‘generations’ array and the rest of your code. If you’re still having trouble, please provide more context or details about the error.

I hope this helps! If you have any more questions, feel free to ask.

Best, Dosu

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

Hi, @gsans,

I’m helping the langchainjs team manage their backlog and am marking this issue as stale. From what I understand, the issue was reported by you regarding a TypeError in the call method of the base.js file in the langchain package. There were discussions about potential fixes, including upgrading to the latest version of the package and investigating Angular build settings. However, it seems that the issue remains unresolved, and it’s unclear if it’s still relevant to the latest version of the langchainjs repository.

Could you please confirm if the issue is still relevant to the latest version of the langchainjs repository? If it is, please let the langchainjs team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you for your understanding and contribution to the langchainjs project.