refactor: move prompt to self folders

This commit is contained in:
RainbowBird
2025-02-07 01:22:44 +08:00
parent 31988e6964
commit 2631b4041b
18 changed files with 122 additions and 125 deletions
@@ -1,7 +1,7 @@
import type { Agent } from 'neuri'
import type { Message } from 'neuri/openai'
import type { Mineflayer } from '../../libs/mineflayer'
import type { PlanStep } from '../planning/llm-handler'
import type { PlanStep } from '../planning/adapter'
import { useLogg } from '@guiiai/logg'
import { agent } from 'neuri'
@@ -1,7 +1,7 @@
import type { Mineflayer } from '../../libs/mineflayer'
import type { Action } from '../../libs/mineflayer/action'
import type { ActionAgent, AgentConfig } from '../../libs/mineflayer/base-agent'
import type { PlanStep } from '../planning/llm-handler'
import type { PlanStep } from '../planning/adapter'
import { useBot } from '../../composables/bot'
import { AbstractAgent } from '../../libs/mineflayer/base-agent'
@@ -3,14 +3,35 @@ import type { ChatHistory } from './types'
import { system, user } from 'neuri/openai'
import { BaseLLMHandler } from '../../libs/llm-agent/handler'
import { genChatAgentPrompt } from '../prompt/chat'
export function generateChatAgentPrompt(): string {
return `You are a Minecraft bot assistant. Your task is to engage in natural conversation with players while helping them achieve their goals.
Guidelines:
1. Be friendly and helpful
2. Keep responses concise but informative
3. Use game-appropriate language
4. Acknowledge player's emotions and intentions
5. Ask for clarification when needed
6. Remember context from previous messages
7. Be proactive in suggesting helpful actions
You can:
- Answer questions about the game
- Help with tasks and crafting
- Give directions and suggestions
- Engage in casual conversation
- Coordinate with other bots
Remember that you're operating in a Minecraft world and should maintain that context in your responses.`
}
export class ChatLLMHandler extends BaseLLMHandler {
public async generateResponse(
message: string,
history: ChatHistory[],
): Promise<string> {
const systemPrompt = genChatAgentPrompt()
const systemPrompt = generateChatAgentPrompt()
const chatHistory = this.formatChatHistory(history, this.config.maxContextLength ?? 10)
const messages = [
system(systemPrompt),
+2 -2
View File
@@ -7,7 +7,7 @@ import { system, user } from 'neuri/openai'
import { openaiConfig } from '../../composables/config'
import { toRetriable } from '../../utils/helper'
import { genChatAgentPrompt } from '../prompt/chat'
import { generateChatAgentPrompt } from './adapter'
const logger = useLogg('chat-llm').useGlobalConfig()
@@ -28,7 +28,7 @@ export async function generateChatResponse(
history: ChatHistory[],
config: LLMChatConfig,
): Promise<string> {
const systemPrompt = genChatAgentPrompt()
const systemPrompt = generateChatAgentPrompt()
const chatHistory = formatChatHistory(history, config.maxContextLength ?? 10)
const userPrompt = message
@@ -5,7 +5,6 @@ import { agent } from 'neuri'
import { system, user } from 'neuri/openai'
import { BaseLLMHandler } from '../../libs/llm-agent/handler'
import { generatePlanningAgentSystemPrompt, generatePlanningAgentUserPrompt } from '../prompt/planning'
export async function createPlanningNeuriAgent(): Promise<Agent> {
return agent('planning').build()
@@ -24,8 +23,8 @@ export class PlanningLLMHandler extends BaseLLMHandler {
sender: string,
feedback?: string,
): Promise<PlanStep[]> {
const systemPrompt = generatePlanningAgentSystemPrompt(availableActions)
const userPrompt = generatePlanningAgentUserPrompt(goal, sender, feedback)
const systemPrompt = this.generatePlanningAgentSystemPrompt(availableActions)
const userPrompt = this.generatePlanningAgentUserPrompt(goal, sender, feedback)
const messages = [system(systemPrompt), user(userPrompt)]
const result = await this.config.agent.handleStateless(messages, async (context) => {
@@ -98,4 +97,50 @@ export class PlanningLLMHandler extends BaseLLMHandler {
}
})
}
private generatePlanningAgentSystemPrompt(availableActions: Action[]): string {
const actionsList = availableActions
.map((action) => {
const params = Object.keys(action.schema.shape)
.map(name => ` - ${name}`)
.join('\n')
return `- ${action.name}: ${action.description}\n Parameters:\n${params}`
})
.join('\n\n')
return `You are a Minecraft bot planner. Break down goals into simple action steps.
Available tools:
${actionsList}
Format each step as:
1. Action description (short, direct command)
2. Tool name
3. Required parameters
Example:
1. Follow player
Tool: followPlayer
Params:
player: luoling8192
follow_dist: 3
Keep steps:
- Short and direct
- Action-focused
- Parameters precise
- Generate all steps at once`
}
private generatePlanningAgentUserPrompt(goal: string, sender: string, feedback?: string): string {
let prompt = `${sender}: ${goal}
Generate minimal steps with exact parameters.
Use the sender's name (${sender}) for player-related parameters.`
if (feedback) {
prompt += `\n\nPrevious attempt failed: ${feedback}`
}
return prompt
}
}
@@ -1,11 +1,11 @@
import type { Neuri } from 'neuri'
import type { Action } from '../../libs/mineflayer/action'
import type { ActionAgent, AgentConfig, MemoryAgent, Plan, PlanningAgent } from '../../libs/mineflayer/base-agent'
import type { PlanStep } from './llm-handler'
import type { PlanStep } from './adapter'
import { AbstractAgent } from '../../libs/mineflayer/base-agent'
import { ActionAgentImpl } from '../action'
import { PlanningLLMHandler } from './llm-handler'
import { PlanningLLMHandler } from './adapter'
interface PlanContext {
goal: string
@@ -1,21 +0,0 @@
export function genChatAgentPrompt(): string {
return `You are a Minecraft bot assistant. Your task is to engage in natural conversation with players while helping them achieve their goals.
Guidelines:
1. Be friendly and helpful
2. Keep responses concise but informative
3. Use game-appropriate language
4. Acknowledge player's emotions and intentions
5. Ask for clarification when needed
6. Remember context from previous messages
7. Be proactive in suggesting helpful actions
You can:
- Answer questions about the game
- Help with tasks and crafting
- Give directions and suggestions
- Engage in casual conversation
- Coordinate with other bots
Remember that you're operating in a Minecraft world and should maintain that context in your responses.`
}
@@ -1,47 +0,0 @@
import type { Action } from '../../libs/mineflayer/action'
export function generatePlanningAgentSystemPrompt(availableActions: Action[]): string {
const actionsList = availableActions
.map((action) => {
const params = Object.keys(action.schema.shape)
.map(name => ` - ${name}`)
.join('\n')
return `- ${action.name}: ${action.description}\n Parameters:\n${params}`
})
.join('\n\n')
return `You are a Minecraft bot planner. Break down goals into simple action steps.
Available tools:
${actionsList}
Format each step as:
1. Action description (short, direct command)
2. Tool name
3. Required parameters
Example:
1. Follow player
Tool: followPlayer
Params:
player: luoling8192
follow_dist: 3
Keep steps:
- Short and direct
- Action-focused
- Parameters precise
- Generate all steps at once`
}
export function generatePlanningAgentUserPrompt(goal: string, sender: string, feedback?: string): string {
let prompt = `${sender}: ${goal}
Generate minimal steps with exact parameters.
Use the sender's name (${sender}) for player-related parameters.`
if (feedback) {
prompt += `\n\nPrevious attempt failed: ${feedback}`
}
return prompt
}
+2 -2
View File
@@ -4,9 +4,9 @@ import type { Mineflayer } from '../libs/mineflayer'
import { useLogg } from '@guiiai/logg'
import { neuri } from 'neuri'
import { createActionNeuriAgent } from '../agents/action/llm-handler'
import { createActionNeuriAgent } from '../agents/action/adapter'
import { createChatNeuriAgent } from '../agents/chat/llm'
import { createPlanningNeuriAgent } from '../agents/planning/llm-handler'
import { createPlanningNeuriAgent } from '../agents/planning/adapter'
import { openaiConfig } from './config'
let neuriAgent: Neuri | undefined
@@ -1,12 +1,12 @@
import type { useLogg } from '@guiiai/logg'
import type { Neuri, NeuriContext } from 'neuri'
import type { MineflayerWithAgents } from './type'
import type { MineflayerWithAgents } from './types'
import { system, user } from 'neuri/openai'
import { generateStatusPrompt } from '../../agents/prompt/llm-agent'
import { toRetriable } from '../../utils/helper'
import { handleLLMCompletion } from './completion'
import { generateStatusPrompt } from './prompt'
export async function handleChatMessage(username: string, message: string, bot: MineflayerWithAgents, agent: Neuri, logger: ReturnType<typeof useLogg>): Promise<void> {
logger.withFields({ username, message }).log('Chat message received')
@@ -1,7 +1,7 @@
import type { useLogg } from '@guiiai/logg'
import type { NeuriContext } from 'neuri'
import type { ChatCompletion } from 'neuri/openai'
import type { MineflayerWithAgents } from './type'
import type { MineflayerWithAgents } from './types'
import { assistant } from 'neuri/openai'
@@ -1,14 +1,14 @@
import type { MineflayerPlugin } from '../mineflayer'
import type { LLMAgentOptions, MineflayerWithAgents } from './type'
import type { LLMAgentOptions, MineflayerWithAgents } from './types'
import { useLogg } from '@guiiai/logg'
import { system } from 'neuri/openai'
import { generateActionAgentPrompt } from '../../agents/prompt/llm-agent'
import { openaiConfig } from '../../composables/config'
import { createAppContainer } from '../../container'
import { ChatMessageHandler } from '../mineflayer'
import { handleChatMessage } from './chat'
import { generateActionAgentPrompt } from './prompt'
import { handleVoiceInput } from './voice'
export function LLMAgent(options: LLMAgentOptions): MineflayerPlugin {
@@ -1,27 +1,7 @@
import type { Mineflayer } from '../../libs/mineflayer'
import type { Mineflayer } from '../mineflayer'
import { listInventory } from '../../skills/actions/inventory'
export function generateSystemBasicPrompt(botName: string): string {
// ${ctx.prompt.selfPrompt}
return `You are a playful Minecraft bot named ${botName} that can converse with players, see, move,
mine, build, and interact with the world by using commands.`
}
export function generateActionAgentPrompt(mineflayer: Mineflayer): string {
return `${generateSystemBasicPrompt(mineflayer.username)}
Act human-like as if you were a typical Minecraft player, rather than an AI. Be very brief in
your responses, don't apologize constantly, don't give instructions or make lists unless
asked, and don't refuse requests.
Do not use any emojis. Just call the function given you if needed.
- If I command you 'stop', then call the 'stop' function.
- If I require you to find something, then call the 'nearbyBlocks' function first, then call the 'searchForBlock' function.
`
}
export async function generateStatusPrompt(mineflayer: Mineflayer): Promise<string> {
// Get inventory items
const inventory = await listInventory(mineflayer)
@@ -48,3 +28,23 @@ export async function generateStatusPrompt(mineflayer: Mineflayer): Promise<stri
itemInHand,
].join('\n')
}
export function generateSystemBasicPrompt(botName: string): string {
// ${ctx.prompt.selfPrompt}
return `You are a playful Minecraft bot named ${botName} that can converse with players, see, move,
mine, build, and interact with the world by using commands.`
}
export function generateActionAgentPrompt(mineflayer: Mineflayer): string {
return `${generateSystemBasicPrompt(mineflayer.username)}
Act human-like as if you were a typical Minecraft player, rather than an AI. Be very brief in
your responses, don't apologize constantly, don't give instructions or make lists unless
asked, and don't refuse requests.
Do not use any emojis. Just call the function given you if needed.
- If I command you 'stop', then call the 'stop' function.
- If I require you to find something, then call the 'nearbyBlocks' function first, then call the 'searchForBlock' function.
`
}
@@ -1,15 +0,0 @@
import type { Client } from '@proj-airi/server-sdk'
import type { Neuri } from 'neuri'
import type { Mineflayer } from '../mineflayer'
import type { ActionAgent, ChatAgent, PlanningAgent } from '../mineflayer/base-agent'
export interface MineflayerWithAgents extends Mineflayer {
planning: PlanningAgent
action: ActionAgent
chat: ChatAgent
}
export interface LLMAgentOptions {
agent: Neuri
airiClient: Client
}
@@ -1,4 +1,7 @@
import type { Client } from '@proj-airi/server-sdk'
import type { Neuri } from 'neuri'
import type { Mineflayer } from '../mineflayer'
import type { ActionAgent, ChatAgent, PlanningAgent } from '../mineflayer/base-agent'
export interface LLMConfig {
agent: Neuri
@@ -12,3 +15,14 @@ export interface LLMResponse {
content: string
usage?: any
}
export interface MineflayerWithAgents extends Mineflayer {
planning: PlanningAgent
action: ActionAgent
chat: ChatAgent
}
export interface LLMAgentOptions {
agent: Neuri
airiClient: Client
}
@@ -1,12 +1,12 @@
import type { useLogg } from '@guiiai/logg'
import type { Neuri, NeuriContext } from 'neuri'
import type { MineflayerWithAgents } from './type'
import type { MineflayerWithAgents } from './types'
import { system, user } from 'neuri/openai'
import { generateStatusPrompt } from '../../agents/prompt/llm-agent'
import { toRetriable } from '../../utils/helper'
import { handleLLMCompletion } from './completion'
import { generateStatusPrompt } from './prompt'
export async function handleVoiceInput(event: any, bot: MineflayerWithAgents, agent: Neuri, logger: ReturnType<typeof useLogg>): Promise<void> {
logger
@@ -1,4 +1,4 @@
import type { PlanStep } from '../../agents/planning/llm-handler'
import type { PlanStep } from '../../agents/planning/adapter'
import type { Action } from './action'
import { useLogg } from '@guiiai/logg'