Skip to content

Commit

Permalink
Agents
Browse files Browse the repository at this point in the history
  • Loading branch information
awhiteside1 committed Sep 26, 2024
1 parent 1465a85 commit a59fec8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
22 changes: 12 additions & 10 deletions packages/agents/src/Agent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { HelloAgent, Task } from "./Agent";
import { Effect, Layer, pipe } from "effect";
import { Effect } from "effect";
import { Schema } from "@effect/schema";
import { AppLayer, LLMServiceLive } from "./services";
import { AppLayer } from "./services";

describe("Agent", () => {
it("should ", {timeout:100000},async () => {
const program = Effect.gen(function* () {
it("should ", { timeout: 100000 }, async () => {
const program = Effect.gen(function* (_) {
const agent = new HelloAgent();
const createTask = Effect.succeed(
new Task(
"Create a greeting for Alex Whiteside, a software architect who enjoys sarcasm and XKCD",
["Be Positive", "Be Creative"],
Schema.Struct({greeting: Schema.String}),
Schema.Struct({ greeting: Schema.String }),
),
);
const result = yield* agent.process(createTask);
console.log(result);
return yield* agent.process(createTask);
});

const ollamaLayer = LLMServiceLive();
const runnable = Effect.provide(program, ollamaLayer);
// const ollamaLayer = LLMServiceLive();
const runnable = Effect.provide(program, AppLayer);

await Effect.runPromiseExit(runnable).then(console.log);
const output = await Effect.runPromise(runnable);
expect(output.data).toMatchObject(
expect.objectContaining({ greeting: expect.stringContaining("Alex") }),
);
});
});
43 changes: 12 additions & 31 deletions packages/agents/src/Agent.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { JSONSchema, Schema } from "@effect/schema";
import { LLMService } from "./services";
import { Effect, pipe, Either } from "effect";
import { Effect, Match, pipe } from "effect";
import { extractJSON, getMdast, stringifyMdast } from "./md";
import { Match } from "effect";
import { get, isArray, unique } from "radash";
import { isSchema, transform } from "@effect/schema/Schema";
import { isSchema } from "@effect/schema/Schema";
import { getSectionByHeading } from "./md/getSection";
import { Root } from "mdast";
import { isRight } from "effect/Either";

export class Task {
constructor(
Expand Down Expand Up @@ -116,11 +115,17 @@ export abstract class Agent {
const llm = yield* LLMService;

const taskReal = yield* task;
console.log(taskReal);
const prompt = generatePrompt(taskReal);
console.log(prompt);
const response = yield* llm.generateText(prompt, "mistral-small");
return parseTaskResponse(taskReal, response);

const mdast = yield* getMdast(response);
const data = Schema.decodeEither(taskReal.format)(
extractJSON(getSectionByHeading(mdast, sections.data.heading)),
);
if (isRight(data)) {
return new TaskResult(data.right, "");
}
return new TaskResult(null, "null");
});
}
}
Expand All @@ -130,27 +135,3 @@ export class HelloAgent extends Agent {
description = "Welcomes people to the project";
}

const parseTaskResponse = (task: Task, response: string) => {
const root = getMdast(response);

const getSection = <T extends Effect.Effect<any, Error, never>>(
heading: string,
transform?: T,
) => {
return pipe(
Effect.try({
try: () => getSectionByHeading(root, heading),
catch: (err) => err,
}),
Effect.map(transform ?? stringifyMdast),
);
};

const data = getSection(sections.data.heading, (node) =>
Schema.decodeEither(task.format)(extractJSON(node)),
);

if (task.format && data && Either.isLeft(data)) throw new Error("Failed");
const unstructured = getSection(sections.unstructured.heading);
return new TaskResult(data, unstructured);
};
5 changes: 3 additions & 2 deletions packages/agents/src/md/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { remark } from "remark";
import remarkGfm from "remark-gfm";
import { Root } from "mdast";
import { Effect } from "effect";

const markdownPipeline = remark().use(remarkGfm);

export const getMdast = (md: string) => {
return markdownPipeline.parse(md);
return Effect.succeed(markdownPipeline.parse(md));
};

export const stringifyMdast = (node: Root)=> markdownPipeline.stringify(node)
export const stringifyMdast = (node: Root)=> Effect.succeed(markdownPipeline.stringify(node))
7 changes: 6 additions & 1 deletion packages/agents/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/// <reference types="vitest" />

import { defineConfig } from "vitest/config";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
plugins: [tsconfigPaths()],
build:{
sourcemap:'inline'

},

test: {

globals: true,
},
});

0 comments on commit a59fec8

Please sign in to comment.