Skip to content

Tracking token usage when using RunnableWithMessageHistory and Streaming #5880

Discussion options

You must be logged in to vote
const llm = new ChatOpenAI({
  modelName: "gpt-3.5-turbo-0125",
  temperature: 0,
  streaming: true,
  streamUsage: true,
});

const prompt = ChatPromptTemplate.fromMessages([
  ["system", system],
  ["placeholder", "{chat_history}"],
  ["human", "{input}"],
]);

const chain = prompt.pipe(llm);

const chatwithMessageHistory = new RunnableWithMessageHistory({
  runnable: chain,
  getMessageHistory: (_sessionId) =>
    new UpstashRedisChatMessageHistory({
      sessionId: "session id goes here",
      sessionTTL: 300,
      config: {
        url: "url goes here",
        token: "token goes here",
      },
    }),
  inputMessagesKey: "input",
  historyMessagesKey: "history",
});

/*Code abov…

Replies: 4 comments 10 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
9 replies
@dosubot
Comment options

@KeenanFernandes2000
Comment options

@dosubot
Comment options

@KeenanFernandes2000
Comment options

@dosubot
Comment options

Comment options

You must be logged in to vote
1 reply
@KeenanFernandes2000
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by KeenanFernandes2000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants