From 914cf06f251a687ecccc882f087cff479ff2ae7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Fran=C3=A7a?= Date: Tue, 19 Nov 2024 19:05:53 -0300 Subject: [PATCH] Refactor decopilot dry (#946) * Update AppContext type to include prompt * Refactor prompt provider in runPrompt.ts * Refactor prompt provider in runSavedPrompts.ts * Refactor prompt provider in llmClientObjects.ts * Refactor prompt provider in assembleComplexPrompt.ts * Refactor handleAttachments function to use anthropic supported method * Update handleAttachments.ts --- decopilot-app/actions/prompt/runPrompt.ts | 12 +- .../actions/prompt/runSavedPrompts.ts | 12 +- decopilot-app/clients/llmClientObjects.ts | 25 +--- decopilot-app/mod.ts | 2 +- decopilot-app/utils/assembleComplexPrompt.ts | 107 ++---------------- decopilot-app/utils/handleAttachments.ts | 4 +- 6 files changed, 17 insertions(+), 145 deletions(-) diff --git a/decopilot-app/actions/prompt/runPrompt.ts b/decopilot-app/actions/prompt/runPrompt.ts index e6411733a..0715ba6d7 100644 --- a/decopilot-app/actions/prompt/runPrompt.ts +++ b/decopilot-app/actions/prompt/runPrompt.ts @@ -1,5 +1,4 @@ -// import { shortcircuit } from "@deco/deco"; -import { callAntropic, callOpenAI } from "../../clients/llmClientObjects.ts"; +import { callAnthropic } from "../../clients/llmClientObjects.ts"; import type { AppContext } from "../../mod.ts"; import type { Attachment, LLMResponseType, Prompt } from "../../types.ts"; @@ -42,15 +41,8 @@ export default async function action( } if (prompt.provider === "Anthropic") { - return await callAntropic(prompt, ctx, attachments ?? []); + return await callAnthropic(prompt, ctx, attachments ?? []); } - if (prompt.provider === "OpenAI") { - return await callOpenAI(prompt, ctx, attachments ?? []); - } - // if (prompt.provider === "Custom") { - // return await callCustomProvider(prompt, ctx, attachments); - // } - throw new Error(`Provider ${prompt.provider} is not supported`); } diff --git a/decopilot-app/actions/prompt/runSavedPrompts.ts b/decopilot-app/actions/prompt/runSavedPrompts.ts index 428691f5e..b844e88a6 100644 --- a/decopilot-app/actions/prompt/runSavedPrompts.ts +++ b/decopilot-app/actions/prompt/runSavedPrompts.ts @@ -1,5 +1,4 @@ -// import { shortcircuit } from "@deco/deco"; -import { callAntropic, callOpenAI } from "../../clients/llmClientObjects.ts"; +import { callAnthropic } from "../../clients/llmClientObjects.ts"; import type { AppContext } from "../../mod.ts"; import type { Attachment, LLMResponseType } from "../../types.ts"; @@ -24,15 +23,8 @@ export default async function action( } if (prompt.provider === "Anthropic") { - return await callAntropic(prompt, ctx, attachments ?? []); + return await callAnthropic(prompt, ctx, attachments ?? []); } - if (prompt.provider === "OpenAI") { - return await callOpenAI(prompt, ctx, attachments ?? []); - } - // if (prompt.provider === "Custom") { - // return await callCustomProvider(prompt, ctx, attachments); - // } - throw new Error(`Provider ${prompt.provider} is not supported`); } diff --git a/decopilot-app/clients/llmClientObjects.ts b/decopilot-app/clients/llmClientObjects.ts index 7fb1685ea..b23993617 100644 --- a/decopilot-app/clients/llmClientObjects.ts +++ b/decopilot-app/clients/llmClientObjects.ts @@ -1,4 +1,3 @@ -// import getAppTools from "../../openai/actions/code.ts"; import { AppContext as AnthropicAppContext } from "../../anthropic/mod.ts"; import { allowedModels } from "../../anthropic/actions/code.ts"; @@ -6,7 +5,7 @@ import { AppContext } from "../mod.ts"; import { Attachment, LLMResponseType, Prompt } from "../types.ts"; import assembleFinalPrompt from "../utils/assembleComplexPrompt.ts"; -export const callAntropic = async ( +export const callAnthropic = async ( prompt: Prompt, ctx: AppContext, attachments?: Attachment[], @@ -72,28 +71,6 @@ export const callAntropic = async ( // logica pra chamar a openai }; -export const callOpenAI = ( - prompt: Prompt, - _ctx: AppContext, - _attachments?: Attachment[], -): LLMResponseType => { - const response: LLMResponseType = { - id: "None", - created: 0, - provider: "OpenAI", // Provider is OpenAI - model: prompt.model, - tools: [""], - llm_response: [{ - message: { - role: "Not Ready", - content: `OpenAI functions still not ready`, - }, - index: 0, - }], - }; - return response; -}; - export type Caller = ( prompt: Prompt, attachments: Attachment[], diff --git a/decopilot-app/mod.ts b/decopilot-app/mod.ts index ba68763ba..6a924795a 100644 --- a/decopilot-app/mod.ts +++ b/decopilot-app/mod.ts @@ -22,7 +22,7 @@ import AnthropicApp from "../anthropic/mod.ts"; // export type AppManifest = ManifestOf; export type App = ReturnType; -export type AppContext = AC; +export type AppContext = AC & { prompt: Prompt }; export interface Props { credentials: Credentials[]; diff --git a/decopilot-app/utils/assembleComplexPrompt.ts b/decopilot-app/utils/assembleComplexPrompt.ts index ba49fa0eb..7beb293be 100644 --- a/decopilot-app/utils/assembleComplexPrompt.ts +++ b/decopilot-app/utils/assembleComplexPrompt.ts @@ -1,71 +1,7 @@ import { Attachment, Prompt, PromptDetails } from "../types.ts"; -import { handleAnthropicAttachments } from "./handleAttachments.ts"; +import { handleAttachments } from "./handleAttachments.ts"; -export function assembleOpenAIPrompt( - mainPrompt: string, - advanced?: PromptDetails, - handledAttachments?: string | null, -): string { - let finalPrompt = mainPrompt; - - if (advanced) { - const { context, examples, restrictions } = advanced; - - if (context) { - finalPrompt += `\n\nContext:\n${context}`; - } - - if (examples) { - finalPrompt += `\n\nExamples:\n${examples}`; - } - - if (handledAttachments) { - finalPrompt += handledAttachments; - } - - if (restrictions) { - finalPrompt += `\n\nRestrictions:\n${restrictions}`; - } - } else if (handledAttachments) { - finalPrompt += handledAttachments; - } - - return finalPrompt; -} - -export function assembleAnthropicPrompt( - mainPrompt: string, - advanced?: PromptDetails, - handledAttachments?: string | null, -): string { - let finalPrompt = mainPrompt; - - if (advanced) { - const { context, examples, restrictions } = advanced; - - if (context) { - finalPrompt += `\n\n\n${context}\n`; - } - - if (examples) { - finalPrompt += `\n\n\n${examples}\n`; - } - - if (handledAttachments) { - finalPrompt += handledAttachments; - } - - if (restrictions) { - finalPrompt += `\n\n\n${restrictions}\n`; - } - } else if (handledAttachments) { - finalPrompt += handledAttachments; - } - - return finalPrompt; -} - -function assembleFallbackPrompt( +export function assemblePrompt( mainPrompt: string, advanced?: PromptDetails, handledAttachments?: string | null, @@ -101,40 +37,13 @@ export default async function assembleFinalPrompt( prompt: Prompt, attachments?: Attachment[], ): Promise { - const { provider, prompt: mainPrompt, advanced } = prompt; - - let finalPrompt = ""; - let handledAttachments = ""; - - if (attachments) { - handledAttachments = await handleAnthropicAttachments(attachments); - } - - switch (provider) { - case "OpenAI": - finalPrompt = assembleOpenAIPrompt( - mainPrompt, - advanced, - handledAttachments, - ); - break; + const { prompt: mainPrompt, advanced } = prompt; - case "Anthropic": - finalPrompt = assembleAnthropicPrompt( - mainPrompt, - advanced, - handledAttachments, - ); - break; - - default: - finalPrompt = assembleFallbackPrompt( - mainPrompt, - advanced, - handledAttachments, - ); - break; - } + const finalPrompt = assemblePrompt( + mainPrompt, + advanced, + attachments ? await handleAttachments(attachments) : "", + ); return finalPrompt; } diff --git a/decopilot-app/utils/handleAttachments.ts b/decopilot-app/utils/handleAttachments.ts index 42309bd05..e56d533d7 100644 --- a/decopilot-app/utils/handleAttachments.ts +++ b/decopilot-app/utils/handleAttachments.ts @@ -1,6 +1,8 @@ import { Attachment, FileURL } from "../types.ts"; -export async function handleAnthropicAttachments( +// currently handles using anthropic suggested method + +export async function handleAttachments( attachments: Attachment[], ): Promise { const treated_Attachments: string[] = [];