Skip to content

Commit

Permalink
feat: global configurable model and reasoning model
Browse files Browse the repository at this point in the history
  • Loading branch information
luoling8192 committed Jan 28, 2025
1 parent e561678 commit 811d700
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
OPENAI_API_BASEURL=''
OPENAI_API_KEY=''
OPENAI_MODEL='deepseek-chat'
OPENAI_REASONING_MODEL='deepseek-reasoner'

BOT_USERNAME=''
BOT_HOSTNAME=''
Expand Down
4 changes: 2 additions & 2 deletions src/agents/action/llm-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { messages, system, user } from 'neuri/openai'
import { beforeAll, describe, expect, it } from 'vitest'

import { initBot, useBot } from '../../composables/bot'
import { botConfig, initEnv } from '../../composables/config'
import { botConfig, initEnv, openaiConfig } from '../../composables/config'
import { createNeuriAgent } from '../../composables/neuri'
import { initLogger } from '../../utils/logger'
import { generateSystemBasicPrompt } from '../prompt/llm-agent.plugin'
Expand All @@ -26,7 +26,7 @@ describe('openAI agent', { timeout: 0 }, () => {
user('Hello, who are you?'),
),
async (c) => {
const completion = await c.reroute('query', c.messages, { model: 'openai/gpt-4o-mini' })
const completion = await c.reroute('query', c.messages, { model: openaiConfig.model })
return await completion?.firstContent()
},
)
Expand Down
6 changes: 3 additions & 3 deletions src/agents/action/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { messages, system, user } from 'neuri/openai'
import { beforeAll, describe, expect, it } from 'vitest'

import { initBot, useBot } from '../../composables/bot'
import { botConfig, initEnv } from '../../composables/config'
import { botConfig, initEnv, openaiConfig } from '../../composables/config'
import { createNeuriAgent } from '../../composables/neuri'
import { sleep } from '../../utils/helper'
import { initLogger } from '../../utils/logger'
Expand All @@ -25,7 +25,7 @@ describe('actions agent', { timeout: 0 }, () => {
system(generateActionAgentPrompt(bot)),
user('What\'s your status?'),
), async (c) => {
const completion = await c.reroute('query', c.messages, { model: 'openai/gpt-4o-mini' })
const completion = await c.reroute('query', c.messages, { model: openaiConfig.model })
return await completion?.firstContent()
})

Expand All @@ -46,7 +46,7 @@ describe('actions agent', { timeout: 0 }, () => {
system(generateActionAgentPrompt(bot)),
user('goToPlayer: luoling8192'),
), async (c) => {
const completion = await c.reroute('action', c.messages, { model: 'openai/gpt-4o-mini' })
const completion = await c.reroute('action', c.messages, { model: openaiConfig.model })

return await completion?.firstContent()
})
Expand Down
3 changes: 2 additions & 1 deletion src/agents/chat/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useLogg } from '@guiiai/logg'
import { agent } from 'neuri'
import { system, user } from 'neuri/openai'

import { openaiConfig } from '../../composables/config'
import { toRetriable } from '../../utils/helper'
import { genChatAgentPrompt } from '../prompt/chat'

Expand Down Expand Up @@ -42,7 +43,7 @@ export async function generateChatResponse(

const handleCompletion = async (c: any): Promise<string> => {
const completion = await c.reroute('chat', c.messages, {
model: config.model ?? 'openai/gpt-4o-mini',
model: config.model ?? openaiConfig.model,
})

if (!completion || 'error' in completion) {
Expand Down
5 changes: 4 additions & 1 deletion src/composables/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface OpenAIConfig {
apiKey: string
baseUrl: string
model: string
reasoningModel: string
}

interface EnvConfig {
Expand All @@ -22,7 +23,8 @@ const defaultConfig: EnvConfig = {
openai: {
apiKey: '',
baseUrl: '',
model: 'openai/gpt-4o-mini',
model: '',
reasoningModel: '',
},
bot: {
username: '',
Expand All @@ -46,6 +48,7 @@ export function initEnv(): void {
apiKey: env.OPENAI_API_KEY || defaultConfig.openai.apiKey,
baseUrl: env.OPENAI_API_BASEURL || defaultConfig.openai.baseUrl,
model: env.OPENAI_MODEL || defaultConfig.openai.model,
reasoningModel: env.OPENAI_REASONING_MODEL || defaultConfig.openai.reasoningModel,
},
bot: {
username: env.BOT_USERNAME || defaultConfig.bot.username,
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/llm-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useLogg } from '@guiiai/logg'
import { assistant, system, user } from 'neuri/openai'

import { generateActionAgentPrompt, generateStatusPrompt } from '../agents/prompt/llm-agent.plugin'
import { openaiConfig } from '../composables/config'
import { createAppContainer } from '../container'
import { ChatMessageHandler } from '../libs/mineflayer/message'
import { toRetriable } from '../utils/helper'
Expand All @@ -28,7 +29,7 @@ async function handleLLMCompletion(context: NeuriContext, bot: MineflayerWithAge
logger.log('rerouting...')

const completion = await context.reroute('action', context.messages, {
model: 'openai/gpt-4o-mini',
model: openaiConfig.model,
}) as ChatCompletion | { error: { message: string } } & ChatCompletion

if (!completion || 'error' in completion) {
Expand Down Expand Up @@ -145,7 +146,7 @@ export function LLMAgent(options: LLMAgentOptions): MineflayerPlugin {
// Create container and get required services
const container = createAppContainer({
neuri: options.agent,
model: 'openai/gpt-4o-mini',
model: openaiConfig.model,
maxHistoryLength: 50,
idleTimeout: 5 * 60 * 1000,
})
Expand Down

0 comments on commit 811d700

Please sign in to comment.