From 66c96591abd1821eae30fc0971a5cd49c0ec8983 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Wed, 7 Jun 2023 12:09:00 -0700 Subject: [PATCH] feat: refactoring llms to subfolder --- src/agentic.ts | 3 +-- src/index.ts | 4 +--- src/{ => llms}/anthropic.ts | 5 +++-- src/llms/index.ts | 3 +++ src/{ => llms}/llm.ts | 13 ++++++++----- src/{ => llms}/openai.ts | 5 +++-- src/tokenizer.ts | 4 ++-- src/tools/metaphor.ts | 8 ++++---- test/_utils.ts | 2 +- test/anthropic.test.ts | 3 ++- test/openai.test.ts | 3 ++- test/serpapi.test.ts | 3 ++- tsconfig.json | 5 ++++- 13 files changed, 36 insertions(+), 25 deletions(-) rename src/{ => llms}/anthropic.ts (96%) create mode 100644 src/llms/index.ts rename src/{ => llms}/llm.ts (97%) rename src/{ => llms}/openai.ts (93%) diff --git a/src/agentic.ts b/src/agentic.ts index 3f23c7a..389e265 100644 --- a/src/agentic.ts +++ b/src/agentic.ts @@ -1,11 +1,10 @@ import * as types from './types' import { defaultOpenAIModel } from './constants' -// import { BaseTask } from './task' import { HumanFeedbackMechanism, HumanFeedbackMechanismCLI } from './human-feedback' -import { OpenAIChatModel } from './openai' +import { OpenAIChatModel } from './llms/openai' export class Agentic { // _taskMap: WeakMap> diff --git a/src/index.ts b/src/index.ts index 0bfce3e..33b3a84 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,6 @@ export * from './agentic' export * from './task' -export * from './llm' -export * from './openai' -export * from './anthropic' +export * from './llms' export * from './tokenizer' export * from './human-feedback' diff --git a/src/anthropic.ts b/src/llms/anthropic.ts similarity index 96% rename from src/anthropic.ts rename to src/llms/anthropic.ts index b1dd008..87e8e67 100644 --- a/src/anthropic.ts +++ b/src/llms/anthropic.ts @@ -2,8 +2,9 @@ import * as anthropic from '@anthropic-ai/sdk' import { type SetOptional } from 'type-fest' import { ZodTypeAny, z } from 'zod' -import * as types from './types' -import { defaultAnthropicModel } from './constants' +import * as types from '@/types' +import { defaultAnthropicModel } from '@/constants' + import { BaseChatModel } from './llm' const defaultStopSequences = [anthropic.HUMAN_PROMPT] diff --git a/src/llms/index.ts b/src/llms/index.ts new file mode 100644 index 0000000..e2425cb --- /dev/null +++ b/src/llms/index.ts @@ -0,0 +1,3 @@ +export * from './llm' +export * from './openai' +export * from './anthropic' diff --git a/src/llm.ts b/src/llms/llm.ts similarity index 97% rename from src/llm.ts rename to src/llms/llm.ts index 09a2db1..4004612 100644 --- a/src/llm.ts +++ b/src/llms/llm.ts @@ -5,18 +5,18 @@ import { type SetRequired } from 'type-fest' import { ZodRawShape, ZodTypeAny, z } from 'zod' import { printNode, zodToTs } from 'zod-to-ts' -import * as types from './types' -import { BaseTask } from './task' -import { getCompiledTemplate } from './template' +import * as types from '@/types' +import { BaseTask } from '@/task' +import { getCompiledTemplate } from '@/template' import { Tokenizer, getModelNameForTiktoken, getTokenizerForModel -} from './tokenizer' +} from '@/tokenizer' import { extractJSONArrayFromString, extractJSONObjectFromString -} from './utils' +} from '@/utils' export abstract class BaseLLM< TInput extends ZodRawShape | ZodTypeAny = z.ZodVoid, @@ -317,6 +317,8 @@ export abstract class BaseChatModel< tokensPerName = 1 } else { // TODO + tokensPerMessage = 4 + tokensPerName = -1 } const numTokensPerMessage = await pMap( @@ -342,6 +344,7 @@ export abstract class BaseChatModel< } ) + // TODO numTokensTotal += 3 // every reply is primed with <|start|>assistant<|message|> return { numTokensTotal, numTokensPerMessage } diff --git a/src/openai.ts b/src/llms/openai.ts similarity index 93% rename from src/openai.ts rename to src/llms/openai.ts index 9785341..2d98804 100644 --- a/src/openai.ts +++ b/src/llms/openai.ts @@ -1,8 +1,9 @@ import { type SetOptional } from 'type-fest' import { ZodTypeAny, z } from 'zod' -import * as types from './types' -import { defaultOpenAIModel } from './constants' +import * as types from '@/types' +import { defaultOpenAIModel } from '@/constants' + import { BaseChatModel } from './llm' export class OpenAIChatModel< diff --git a/src/tokenizer.ts b/src/tokenizer.ts index 3c4408b..2a02da0 100644 --- a/src/tokenizer.ts +++ b/src/tokenizer.ts @@ -68,8 +68,8 @@ export async function getTokenizerForEncoding( encoding: TiktokenEncoding, options?: { signal?: AbortSignal - extendedSpecialTokens?: Record timeoutMs?: number + extendedSpecialTokens?: Record } ) { const tiktokenBPE = await getTiktokenBPE(encoding, options) @@ -81,8 +81,8 @@ export async function getTokenizerForModel( model: string, options?: { signal?: AbortSignal - extendedSpecialTokens?: Record timeoutMs?: number + extendedSpecialTokens?: Record } ) { const modelName = getModelNameForTiktoken(model) diff --git a/src/tools/metaphor.ts b/src/tools/metaphor.ts index 0680ab5..45155f1 100644 --- a/src/tools/metaphor.ts +++ b/src/tools/metaphor.ts @@ -1,9 +1,9 @@ import { z } from 'zod' -import * as types from '../types' -import { Agentic } from '../agentic' -import { MetaphorClient } from '../services/metaphor' -import { BaseTask } from '../task' +import * as types from '@/types' +import { Agentic } from '@/agentic' +import { MetaphorClient } from '@/services/metaphor' +import { BaseTask } from '@/task' export const MetaphorSearchToolInputSchema = z.object({ query: z.string(), diff --git a/test/_utils.ts b/test/_utils.ts index 411f75d..890d729 100644 --- a/test/_utils.ts +++ b/test/_utils.ts @@ -7,7 +7,7 @@ import Keyv from 'keyv' import { OpenAIClient } from 'openai-fetch' import pMemoize from 'p-memoize' -import { Agentic } from '../src' +import { Agentic } from '@/agentic' export const fakeOpenAIAPIKey = 'fake-openai-api-key' export const fakeAnthropicAPIKey = 'fake-anthropic-api-key' diff --git a/test/anthropic.test.ts b/test/anthropic.test.ts index c7f19a1..00f4e2d 100644 --- a/test/anthropic.test.ts +++ b/test/anthropic.test.ts @@ -1,7 +1,8 @@ import test from 'ava' import { expectTypeOf } from 'expect-type' -import { AnthropicChatModel } from '../src' +import { AnthropicChatModel } from '@/llms/anthropic' + import { createTestAgenticRuntime } from './_utils' test('AnthropicChatModel ⇒ string output', async (t) => { diff --git a/test/openai.test.ts b/test/openai.test.ts index 398f437..7034b65 100644 --- a/test/openai.test.ts +++ b/test/openai.test.ts @@ -2,7 +2,8 @@ import test from 'ava' import { expectTypeOf } from 'expect-type' import { z } from 'zod' -import { OpenAIChatModel } from '../src' +import { OpenAIChatModel } from '@/llms/openai' + import { createTestAgenticRuntime } from './_utils' test('OpenAIChatModel ⇒ string output', async (t) => { diff --git a/test/serpapi.test.ts b/test/serpapi.test.ts index 2f5ddad..5adf485 100644 --- a/test/serpapi.test.ts +++ b/test/serpapi.test.ts @@ -1,6 +1,7 @@ import test from 'ava' -import { SerpAPIClient } from '../src/services/serpapi' +import { SerpAPIClient } from '@/services/serpapi' + import './_utils' test('SerpAPIClient.search', async (t) => { diff --git a/tsconfig.json b/tsconfig.json index 4b278db..f18422b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,10 @@ "jsx": "preserve", "baseUrl": ".", "outDir": "build", - "noEmit": true + "noEmit": true, + "paths": { + "@/*": ["./src/*"] + } }, "exclude": ["node_modules", "build"], "include": ["**/*.ts", "**/*.tsx"]