From 566ce1fd7a29a91ff121882161454363043685e5 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Wed, 14 Jun 2023 21:20:47 -0700 Subject: [PATCH] fix: tests --- src/llms/chat.ts | 15 +++-- src/logger.ts | 108 +++++++++++++++++++++++++++++++-- src/types.ts | 2 +- test/llms/openai-tools.test.ts | 4 ++ 4 files changed, 119 insertions(+), 10 deletions(-) diff --git a/src/llms/chat.ts b/src/llms/chat.ts index 615336c..b08573e 100644 --- a/src/llms/chat.ts +++ b/src/llms/chat.ts @@ -219,12 +219,17 @@ export abstract class BaseChatCompletion< let output: any do { + const debugInfo: any = { + ...this._modelParams, + messages + } + + if (functions) { + debugInfo.functions = functions.map((f: any) => f?.name).filter(Boolean) + } + this._logger.info( - { - ...this._modelParams, - messages, - functions: functions.map((f) => f.name) - }, + debugInfo, `>>> Task createChatCompletion "${this.nameForHuman}"` ) diff --git a/src/logger.ts b/src/logger.ts index 6bfb2bb..287e587 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -1,11 +1,111 @@ -import * as pino from 'pino' -import pretty from 'pino-pretty' +import { pino } from 'pino' +import pinoPretty from 'pino-pretty' -export const defaultLogger = pino.pino( +export type Level = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' +export type LevelWithSilent = Level | 'silent' + +interface LogFn { + (obj: T, msg?: string, ...args: any[]): void + (obj: unknown, msg?: string, ...args: any[]): void + (msg: string, ...args: any[]): void +} + +// these types are taken from `pino` +export interface Logger { + /** + * Set this property to the desired logging level. In order of priority, available levels are: + * + * - 'fatal' + * - 'error' + * - 'warn' + * - 'info' + * - 'debug' + * - 'trace' + * + * The logging level is a __minimum__ level. For instance if `logger.level` is `'info'` then all `'fatal'`, `'error'`, `'warn'`, + * and `'info'` logs will be enabled. + * + * You can pass `'silent'` to disable logging. + */ + level: LevelWithSilent | string + + /** + * Log at `'fatal'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + fatal: LogFn + + /** + * Log at `'error'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + error: LogFn + + /** + * Log at `'warn'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + warn: LogFn + + /** + * Log at `'info'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + info: LogFn + + /** + * Log at `'debug'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + debug: LogFn + + /** + * Log at `'trace'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + trace: LogFn + + /** + * Noop function. + */ + silent: LogFn +} + +export const defaultLogger: Logger = pino( { level: process.env.LOG_LEVEL || 'info' }, - pretty({ + pinoPretty({ sync: true, colorize: true }) diff --git a/src/types.ts b/src/types.ts index a2908ae..da5e8ee 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,11 +2,11 @@ import * as openai from '@agentic/openai-fetch' import * as anthropic from '@anthropic-ai/sdk' import ky from 'ky' import type { Options as RetryOptions } from 'p-retry' -import type { Logger } from 'pino' import type { JsonObject, JsonValue } from 'type-fest' import { SafeParseReturnType, ZodType, ZodTypeAny, output, z } from 'zod' import type { Agentic } from './agentic' +import type { Logger } from './logger' import type { BaseTask } from './task' export { openai } diff --git a/test/llms/openai-tools.test.ts b/test/llms/openai-tools.test.ts index 6b9f6f8..8efc658 100644 --- a/test/llms/openai-tools.test.ts +++ b/test/llms/openai-tools.test.ts @@ -32,6 +32,10 @@ test('OpenAIChatCompletion - tools - calculator', async (t) => { }) test('OpenAIChatCompletion - tools - weather', async (t) => { + if (!process.env.WEATHER_API_KEY) { + return t.pass() + } + t.timeout(2 * 60 * 1000) const agentic = createTestAgenticRuntime()