Skip to content

Commit

Permalink
Merge pull request #96 from reworkd/feat/refactor_interfaces_and_types
Browse files Browse the repository at this point in the history
feat: add interfaces, types and constants file
  • Loading branch information
asim-shrestha authored Apr 14, 2023
2 parents 7b8d84f + 45ac2d2 commit c709954
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 57 deletions.
25 changes: 10 additions & 15 deletions src/components/AutonomousAgent.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Message } from "./ChatWindow";
import axios from "axios";
import type { ModelSettings } from "../utils/types";

class AutonomousAgent {
name: string;
goal: string;
tasks: string[] = [];
customApiKey: string;
customModelName: string;
modelSettings: ModelSettings;
isRunning = true;
sendMessage: (message: Message) => void;
shutdown: () => void;
Expand All @@ -17,15 +17,13 @@ class AutonomousAgent {
goal: string,
addMessage: (message: Message) => void,
shutdown: () => void,
customApiKey: string,
customModelName: string
modelSettings: ModelSettings
) {
this.name = name;
this.goal = goal;
this.sendMessage = addMessage;
this.shutdown = shutdown;
this.customApiKey = customApiKey;
this.customModelName = customModelName;
this.modelSettings = modelSettings;
}

async run() {
Expand All @@ -42,7 +40,7 @@ class AutonomousAgent {
} catch (e) {
console.log(e);
this.sendErrorMessage(
this.customApiKey !== ""
this.modelSettings.customApiKey !== ""
? `ERROR retrieving initial tasks array. Make sure your API key is not the free tier, make your goal more clear, or revise your goal such that it is within our model's policies to run. Shutting Down.`
: `ERROR retrieving initial tasks array. Retry, make your goal more clear, or revise your goal such that it is within our model's policies to run. Shutting Down.`
);
Expand Down Expand Up @@ -70,7 +68,7 @@ class AutonomousAgent {
}

this.numLoops += 1;
const maxLoops = this.customApiKey === "" ? 4 : 25;
const maxLoops = this.modelSettings.customApiKey === "" ? 4 : 25;
if (this.numLoops > maxLoops) {
this.sendLoopMessage();
this.shutdown();
Expand Down Expand Up @@ -120,8 +118,7 @@ class AutonomousAgent {

async getInitialTasks(): Promise<string[]> {
const res = await axios.post(`/api/chain`, {
customApiKey: this.customApiKey,
customModelName: this.customModelName,
modelSettings: this.modelSettings,
goal: this.goal,
});

Expand All @@ -134,8 +131,7 @@ class AutonomousAgent {
result: string
): Promise<string[]> {
const res = await axios.post(`/api/create`, {
customApiKey: this.customApiKey,
customModelName: this.customModelName,
modelSettings: this.modelSettings,
goal: this.goal,
tasks: this.tasks,
lastTask: currentTask,
Expand All @@ -147,8 +143,7 @@ class AutonomousAgent {

async executeTask(task: string): Promise<string> {
const res = await axios.post(`/api/execute`, {
customApiKey: this.customApiKey,
customModelName: this.customModelName,
modelSettings: this.modelSettings,
goal: this.goal,
task: task,
});
Expand All @@ -168,7 +163,7 @@ class AutonomousAgent {
this.sendMessage({
type: "system",
value:
this.customApiKey !== ""
this.modelSettings.customApiKey !== ""
? `This agent has been running for too long (25 Loops). To save your wallet, and our infrastructure costs, this agent is shutting down. In the future, the number of iterations will be configurable.`
: "We're sorry, because this is a demo, we cannot have our agents running for too long. Note, if you desire longer runs, please provide your own API key in Settings. Shutting down.",
});
Expand Down
15 changes: 3 additions & 12 deletions src/pages/api/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,16 @@ import {
} from "../../utils/chain";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import type { RequestBody } from "../../utils/interfaces";

export const config = {
runtime: "edge",
};

interface RequestBody {
customApiKey: string;
customModelName: string;
goal: string;
}

const handler = async (request: NextRequest) => {
try {
const { customApiKey, customModelName, goal } =
(await request.json()) as RequestBody;
const completion = await startGoalAgent(
createModel({ customApiKey, customModelName }),
goal
);
const { modelSettings, goal } = (await request.json()) as RequestBody;
const completion = await startGoalAgent(createModel(modelSettings), goal);

const newTasks = extractArray(completion.text as string).filter(
realTasksFilter
Expand Down
19 changes: 8 additions & 11 deletions src/pages/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,23 @@ import {
} from "../../utils/chain";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import type { RequestBody } from "../../utils/interfaces";

export const config = {
runtime: "edge",
};

interface RequestBody {
customApiKey: string;
customModelName: string;
goal: string;
tasks: string[];
lastTask: string;
result: string;
}

const handler = async (request: NextRequest) => {
try {
const { customApiKey, customModelName, goal, tasks, lastTask, result } =
const { modelSettings, goal, tasks, lastTask, result } =
(await request.json()) as RequestBody;

if (tasks === undefined || lastTask === undefined || result === undefined) {
return;
}

const completion = await executeCreateTaskAgent(
createModel({ customApiKey, customModelName }),
createModel(modelSettings),
goal,
tasks,
lastTask,
Expand Down
17 changes: 8 additions & 9 deletions src/pages/api/execute.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { createModel, executeTaskAgent } from "../../utils/chain";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import type { RequestBody } from "../../utils/interfaces";

interface RequestBody {
customApiKey: string;
customModelName: string;
goal: string;
task: string;
}
export const config = {
runtime: "edge",
};

const handler = async (request: NextRequest) => {
try {
const { customApiKey, customModelName, goal, task } =
(await request.json()) as RequestBody;
const { modelSettings, goal, task } = (await request.json()) as RequestBody;

if (task === undefined) {
return;
}

const completion = await executeTaskAgent(
createModel({ customApiKey, customModelName }),
createModel(modelSettings),
goal,
task
);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ const Home: NextPage = () => {
const handleNewGoal = () => {
const addMessage = (message: Message) =>
setMessages((prev) => [...prev, message]);

const agent = new AutonomousAgent(
name,
goalInput,
addMessage,
() => setAgent(null),
customApiKey,
customModelName
{ customApiKey, customModelName }
);
setAgent(agent);
agent.run().then(console.log).catch(console.error);
Expand Down
15 changes: 7 additions & 8 deletions src/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { LLMChain } from "langchain/chains";
import type { ModelSettings } from "./types";
import { GPT_35_TURBO } from "./constants";

type ModelOptions = {
customApiKey: string;
customModelName: string;
};

export const createModel = (opts: ModelOptions) =>
export const createModel = (settings: ModelSettings) =>
new OpenAI({
openAIApiKey:
opts.customApiKey === "" ? process.env.OPENAI_API_KEY : opts.customApiKey,
settings.customApiKey === ""
? process.env.OPENAI_API_KEY
: settings.customApiKey,
temperature: 0.9,
modelName:
opts.customModelName === "" ? "gpt-3.5-turbo" : opts.customModelName,
settings.customModelName === "" ? GPT_35_TURBO : settings.customModelName,
maxTokens: 300,
});

Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const GPT_35_TURBO = "gpt-3.5-turbo";
10 changes: 10 additions & 0 deletions src/utils/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { ModelSettings } from "./types";

export interface RequestBody {
modelSettings: ModelSettings;
goal: string;
task?: string;
tasks?: string[];
lastTask?: string;
result?: string;
}
4 changes: 4 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ModelSettings = {
customApiKey: string;
customModelName: string;
};

1 comment on commit c709954

@vercel
Copy link

@vercel vercel bot commented on c709954 Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.