1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

langchainでChatGPT APIの使用トークン(llmOutput)が返ってこない

Posted at

つまった問題

langchain.jsでChatGPTAPIを利用したチャットボットを作る際に、トークンをかぞえたかったので以下のようにコールバックを設定していた。

import { OpenAI } from "@langchain/openai";

let totalCompletionTokens = 0;
let totalPromptTokens = 0;
let totalTokens = 0;

const model = new OpenAI({
		openAIApiKey: process.env.OPEN_AI_API_KEY, 
		modelName: "gpt-3.5-turbo",
        callbacks: [
            {
                handleLLMEnd(output) {
                    console.log(JSON.stringify(output, null, 2));
                    totalCompletionTokens += output.llmOutput?.tokenUsage?.completionTokens ?? 0;
                    totalPromptTokens += output.llmOutput?.tokenUsage?.promptTokens ?? 0;
                    totalTokens += output.llmOutput?.tokenUsage?.totalTokens ?? 0;
                },
            },
        ],
	});

 //以下省略

しかし、ログを確認するとoutputgenetationsだけが存在しており、llmOutputが含まれていなかった。

{
  "generations": [
    [
      {
        "text": "やほー"
      }
    ]
  ]
}

llmOutputがないと、以下の変数たちにtoken数がカウントアップできない。

totalCompletionTokens += output.llmOutput?.tokenUsage?.completionTokens ?? 0;
totalPromptTokens += output.llmOutput?.tokenUsage?.promptTokens ?? 0;
totalTokens += output.llmOutput?.tokenUsage?.totalTokens ?? 0;

解決策

langchainjsのissueですでに聞かれてた。

I think the reason is that the GPT-3.5-turbo model can only be used for Chat models.
I had to update my old code from 'OpenAI' to 'ChatOpenAI', and that fixed the issue.

つまり、OpenAIをインポートしていたのを

import { OpenAI } from "@langchain/openai";
//・・・
const model = new OpenAI({
//・・・

ChatOpenAIをインポートするように変えるだけみたいです。

import { ChatOpenAI } from "@langchain/openai";
//・・・
const model = new ChatOpenAI({
//・・・

結果

llmOutputが返ってきた。(途中でConversationSammaryを使うようにしたから結果の対称性が微妙ですみません。)

{
  "generations": [
    [
      {
        "text": "The human greets the AI with \"やほー.\" The AI responds enthusiastically with \"やほー!\"",
        "message": {
          "lc": 1,
          "type": "constructor",
          "id": [
            "langchain_core",
            "messages",
            "AIMessage"
          ],
          "kwargs": {
            "content": "The human greets the AI with \"やほー.\" The AI responds enthusiastically with \"やほー!\"",
            "additional_kwargs": {}
          }
        },
        "generationInfo": {
          "finish_reason": "stop"
        }
      }
    ]
  ],
  "llmOutput": {
    "tokenUsage": {
      "completionTokens": 25,
      "promptTokens": 150,
      "totalTokens": 175
    }
  }
}

参考記事

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?