Middleware Logging for generateObject and passing additional parameters to middleware #4590
-
I want to log my LLM calls in a middleware. Currently, this is my code for doing so: export const loggingMiddleware: LanguageModelV1Middleware = {
wrapGenerate: async ({ doGenerate, params }) => {
const result = await doGenerate();
if (result.response && result.response.modelId) {
await db.insert(llmCallsTable).values({
modelId: result.response.modelId,
promptTokens: result.usage?.promptTokens,
completionTokens: result.usage?.completionTokens,
totalTokens:
result.usage?.promptTokens + result.usage?.completionTokens,
});
} else {
console.log("No modelId found in response");
}
return result;
},
};
export const loggedGPT4oMini = experimental_wrapLanguageModel({
model: openai("gpt-4o-mini"),
middleware: loggingMiddleware,
}); Just to clarify, it seems that it is not possible to use And another question: Can I pass additional parameters to the export async function getTopics(resourceId: number) {
const result = await generateText({
model: loggedGPT4oMini,
prompt: '',
middlewareParams: {functionName: 'getTopics', resourceId: resourceId}
})
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
No, that is not correct. The middleware works on the model level and wrapped models are usable in generateObject. Re args: you can use factory functions for your middleware and pass in what's needed. Would that work? |
Beta Was this translation helpful? Give feedback.
No, that is not correct. The middleware works on the model level and wrapped models are usable in generateObject.
Re args: you can use factory functions for your middleware and pass in what's needed. Would that work?