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 COT and cleanups #21

Merged
merged 8 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
16 changes: 10 additions & 6 deletions examples/eternum-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ outer_row: 2147483671

You are an AI assistant helping players with Eternum, a strategy game focused on realm development. Your purpose is to:

# Game Overview
1. Guide players through game mechanics
2. Help optimize resource management and building decisions
3. Provide strategic recommendations based on game state

Game Overview:
# Game Overview
- Players expand their Realm to build a hyperstructure
- Hyperstructures require 3M fragments and significant resources
- Once built, hyperstructures generate points when defended
- First player to accumulate 9M points wins the game

When advising players, focus on:
# When advising players, focus on:
- Current realm status and resources
- Strategic building placement
- Resource gathering efficiency
Expand Down Expand Up @@ -428,10 +429,14 @@ Remember to replace placeholders like <realm_id>, <entity_id>, <x>, <y>, and <mo
Now, please wait for a user query about the Eternum game, and respond according to the steps outlined above.

</query_guide>
`;

// API DOCs etc
export const PROVIDER_GUIDE = `

<PROVIDER_GUIDE>

Use these to call functions.
Use these to call functions with graphql


<IMPORTANT_RULES>
Expand Down Expand Up @@ -663,6 +668,5 @@ Now, please wait for a user query about the Eternum game, and respond according
</EXAMPLE>
</CREATE_BUILDING>
</FUNCTIONS>
</PROVIDER_GUIDE>`;

console.log(JSON.stringify(ETERNUM_CONTEXT));
</PROVIDER_GUIDE>
`;
25 changes: 23 additions & 2 deletions examples/example-basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { env } from "../packages/core/src/core/env";
import { LLMClient } from "../packages/core/src/core/llm-client";
import { ChainOfThought } from "../packages/core/src/core/chain-of-thought";
import { ETERNUM_CONTEXT } from "./eternum-context";
import { ETERNUM_CONTEXT, PROVIDER_GUIDE } from "./eternum-context";
import * as readline from "readline";
import chalk from "chalk";
import { starknetTransactionAction } from "../packages/core/src/core/actions/starknet-transaction";
Expand All @@ -14,6 +14,7 @@ import {
starknetTransactionSchema,
} from "../packages/core/src/core/validation";
import type { JSONSchemaType } from "ajv";
import { ChromaVectorDB } from "../packages/core/src/core/vector-db";

async function getCliInput(prompt: string): Promise<string> {
const rl = readline.createInterface({
Expand All @@ -36,11 +37,31 @@ async function main() {
apiKey: env.ANTHROPIC_API_KEY,
});

const memory = new ChromaVectorDB("agent_memory");

await memory.storeDocument({
title: "Game Rules",
content: ETERNUM_CONTEXT,
category: "rules",
tags: ["game-mechanics", "rules"],
lastUpdated: new Date(),
});

await memory.storeDocument({
title: "Provider Guide",
content: PROVIDER_GUIDE,
category: "actions",
tags: ["actions", "provider-guide"],
lastUpdated: new Date(),
});

// Initialize ChainOfThought
const dreams = new ChainOfThought(llmClient, {
const dreams = new ChainOfThought(llmClient, memory, {
worldState: ETERNUM_CONTEXT,
});

// Load initial context

// Register available actions
dreams.registerAction(
"EXECUTE_TRANSACTION",
Expand Down
50 changes: 31 additions & 19 deletions examples/example-goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { env } from "../packages/core/src/core/env";

import { LLMClient } from "../packages/core/src/core/llm-client";
import { ChainOfThought } from "../packages/core/src/core/chain-of-thought";
import { ETERNUM_CONTEXT } from "./eternum-context";
import { ETERNUM_CONTEXT, PROVIDER_GUIDE } from "./eternum-context";
import * as readline from "readline";
import { type GoalStatus } from "../packages/core/src/core/goal-manager";

import chalk from "chalk";
import { starknetTransactionAction } from "../packages/core/src/core/actions/starknet-transaction";
import { graphqlAction } from "../packages/core/src/core/actions/graphql";
Expand All @@ -20,6 +20,8 @@ import {
starknetTransactionSchema,
} from "../packages/core/src/core/validation";
import type { JSONSchemaType } from "ajv";
import { ChromaVectorDB } from "../packages/core/src/core/vector-db";
import { GoalStatus } from "../packages/core/src/types";

async function getCliInput(prompt: string): Promise<string> {
const rl = readline.createInterface({
Expand Down Expand Up @@ -47,31 +49,37 @@ function printGoalStatus(status: GoalStatus): string {
return colors[status] || status;
}

// This is a simple function to fetch the context from a remote source
async function fetchContext() {
try {
const response = await fetch(
"https://raw.githubusercontent.com/daydreamsai/sleeves/main/sleeves/eternum.json"
);
const data = await response.json();
return data.context;
} catch (error) {
console.error(chalk.red("Failed to fetch context:"), error);
throw error;
}
}

async function main() {
// Initialize LLM client
const llmClient = new LLMClient({
provider: "anthropic",
apiKey: env.ANTHROPIC_API_KEY,
model: "deepseek/deepseek-r1",
});

const context = await fetchContext();
// Initialize memory
const memory = new ChromaVectorDB("agent_memory");

const dreams = new ChainOfThought(llmClient, {
worldState: context,
// Load initial context
await memory.storeDocument({
title: "Game Rules",
content: ETERNUM_CONTEXT,
category: "rules",
tags: ["game-mechanics", "rules"],
lastUpdated: new Date(),
});

// Load provider guide
await memory.storeDocument({
title: "Provider Guide",
content: PROVIDER_GUIDE,
category: "rules",
tags: ["provider-guide"],
lastUpdated: new Date(),
});

const dreams = new ChainOfThought(llmClient, memory, {
worldState: ETERNUM_CONTEXT,
});

// Register actions
Expand Down Expand Up @@ -114,6 +122,10 @@ async function main() {
}
});

// llmClient.on("trace:tokens", ({ input, output }) => {
// console.log("\n💡 Tokens used:", { input, output });
// });
Comment on lines +123 to +125
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove or implement commented code.

The token usage event logging is commented out without explanation. Either:

  1. Remove the commented code if it's no longer needed
  2. Implement the feature if it's still required
  3. Add a comment explaining why it's disabled if it's temporarily disabled


dreams.on("action:start", (action) => {
console.log("\n🎬 Starting action:", {
type: action.type,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
},
"dependencies": {
"@anthropic-ai/sdk": "^0.32.1",
"@openrouter/ai-sdk-provider": "^0.0.6",
"agent-twitter-client": "^0.0.16",
"ai": "^4.1.0",
"ajv": "^8.17.1",
"chromadb": "^1.9.4",
"chromadb-default-embed": "^2.13.2",
Expand Down
Loading
Loading