Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor decopilot dry #946

Merged
merged 7 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions decopilot-app/actions/prompt/runPrompt.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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`);
}
12 changes: 2 additions & 10 deletions decopilot-app/actions/prompt/runSavedPrompts.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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`);
}
25 changes: 1 addition & 24 deletions decopilot-app/clients/llmClientObjects.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// import getAppTools from "../../openai/actions/code.ts";
import { AppContext as AnthropicAppContext } from "../../anthropic/mod.ts";
import { allowedModels } from "../../anthropic/actions/code.ts";

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[],
Expand Down Expand Up @@ -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[],
Expand Down
2 changes: 1 addition & 1 deletion decopilot-app/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import AnthropicApp from "../anthropic/mod.ts";
// export type AppManifest = ManifestOf<App>;

export type App = ReturnType<typeof Decopilot>;
export type AppContext = AC<App>;
export type AppContext = AC<App> & { prompt: Prompt };

export interface Props {
credentials: Credentials[];
Expand Down
107 changes: 8 additions & 99 deletions decopilot-app/utils/assembleComplexPrompt.ts
Original file line number Diff line number Diff line change
@@ -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<Context>\n${context}\n</Context>`;
}

if (examples) {
finalPrompt += `\n\n<Examples>\n${examples}\n</Examples>`;
}

if (handledAttachments) {
finalPrompt += handledAttachments;
}

if (restrictions) {
finalPrompt += `\n\n<Restrictions>\n${restrictions}\n</Restrictions>`;
}
} else if (handledAttachments) {
finalPrompt += handledAttachments;
}

return finalPrompt;
}

function assembleFallbackPrompt(
export function assemblePrompt(
mainPrompt: string,
advanced?: PromptDetails,
handledAttachments?: string | null,
Expand Down Expand Up @@ -101,40 +37,13 @@ export default async function assembleFinalPrompt(
prompt: Prompt,
attachments?: Attachment[],
): Promise<string> {
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;
}
4 changes: 3 additions & 1 deletion decopilot-app/utils/handleAttachments.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
const treated_Attachments: string[] = [];
Expand Down
Loading