From 6dbf97bf99b211b5be09af5559b572cc2be92a75 Mon Sep 17 00:00:00 2001 From: Ehsan Ziya Date: Fri, 6 Oct 2023 21:16:17 +0100 Subject: [PATCH] Refactor: Extract combine function --- src/handlers/anthropic.ts | 5 ++--- src/handlers/cohere.ts | 5 ++--- src/handlers/ollama.ts | 11 ++--------- src/utils/combinePrompts.ts | 7 +++++++ 4 files changed, 13 insertions(+), 15 deletions(-) create mode 100644 src/utils/combinePrompts.ts diff --git a/src/handlers/anthropic.ts b/src/handlers/anthropic.ts index 7c9ac77..ae70de2 100644 --- a/src/handlers/anthropic.ts +++ b/src/handlers/anthropic.ts @@ -10,11 +10,10 @@ import { StreamingChunk, Message, } from '../types'; +import { combinePrompts } from '../utils/combinePrompts'; function toAnthropicPrompt(messages: Message[]): string { - const textsCombined = messages.reduce((acc, message) => { - return (acc += message.content); - }, ''); + const textsCombined = combinePrompts(messages); return `${Anthropic.HUMAN_PROMPT} ${textsCombined}${Anthropic.AI_PROMPT}`; } diff --git a/src/handlers/cohere.ts b/src/handlers/cohere.ts index 5c155c1..e15c5f7 100644 --- a/src/handlers/cohere.ts +++ b/src/handlers/cohere.ts @@ -9,6 +9,7 @@ import { StreamingChunk, } from '../types'; import { cohereResponse, generateResponse } from 'cohere-ai/dist/models'; +import { combinePrompts } from '../utils/combinePrompts'; // eslint-disable-next-line @typescript-eslint/require-await async function* toStream( @@ -44,9 +45,7 @@ export async function CohereHandler( params: HandlerParams, ): Promise { cohere.init(process.env.COHERE_API_KEY!); - const textsCombined = params.messages.reduce((acc, message) => { - return (acc += message.content); - }, ''); + const textsCombined = combinePrompts(params.messages); const config = { model: params.model, diff --git a/src/handlers/ollama.ts b/src/handlers/ollama.ts index 66d87ac..b363e88 100644 --- a/src/handlers/ollama.ts +++ b/src/handlers/ollama.ts @@ -2,11 +2,11 @@ import { HandlerParams, HandlerParamsNotStreaming, HandlerParamsStreaming, - Message, ResultNotStreaming, ResultStreaming, StreamingChunk, } from '../types'; +import { combinePrompts } from '../utils/combinePrompts'; interface OllamaResponseChunk { model: string; @@ -60,13 +60,6 @@ async function* iterateResponse( } } -function combineMessagesToPromit(messages: Message[]): string { - return messages.reduce((acc: string, message: Message) => { - // TODO: Distinguish between the different role types - return (acc += message.content); - }, ''); -} - async function getOllamaResponse( model: string, prompt: string, @@ -102,7 +95,7 @@ export async function OllamaHandler( ): Promise { const baseUrl = params.baseUrl ?? 'http://127.0.0.1:11434'; const model = params.model.split('ollama/')[1]; - const prompt = combineMessagesToPromit(params.messages); + const prompt = combinePrompts(params.messages); const res = await getOllamaResponse(model, prompt, baseUrl); diff --git a/src/utils/combinePrompts.ts b/src/utils/combinePrompts.ts new file mode 100644 index 0000000..9bda792 --- /dev/null +++ b/src/utils/combinePrompts.ts @@ -0,0 +1,7 @@ +import { Message } from '../types'; + +export function combinePrompts(messages: Message[]): string { + return messages.reduce((acc, message) => { + return (acc += message.content); + }, ''); +}