Skip to content

Commit

Permalink
feat(plugin-common): add support for reasoning content in think plugin
Browse files Browse the repository at this point in the history
- Add reasoning_content handling in OpenAIRequester
- Update ChatCompletionResponse type to include reasoning_content
- Modify ThinkTool to use reasoning_content if available
- Add dynamic model selection for think plugin
- Update config schema to support thinkModel option
  • Loading branch information
dingyi222666 committed Feb 11, 2025
1 parent a0c77ba commit 7a2b2d6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/openai-like-adapter/src/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export class OpenAIRequester

let errorCount = 0

let reasoningContent = ''

for await (const event of iterator) {
const chunk = event.data
if (chunk === '[DONE]') {
Expand Down Expand Up @@ -109,6 +111,11 @@ export class OpenAIRequester
defaultRole
)

if (delta.reasoning_content) {
reasoningContent = (reasoningContent +
delta.reasoning_content) as string
}

defaultRole = (
(delta.role?.length ?? 0) > 0 ? delta.role : defaultRole
) as ChatCompletionResponseMessageRoleEnum
Expand Down
1 change: 1 addition & 0 deletions packages/openai-like-adapter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface ChatCompletionResponse {
delta: {
content?: string
role?: string
reasoning_content?: string
function_call?: ChatCompletionRequestMessageToolCall
}
message: ChatCompletionResponseMessage
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface Config extends ChatLunaPlugin.Config {
codeSandboxAPIKey: string
knowledge: boolean
knowledgeId: string[]
thinkModel: string
actionsList: {
name: string
description: string
Expand Down Expand Up @@ -89,6 +90,13 @@ export const Config: Schema<Config> = Schema.intersect([
}),
Schema.object({})
]),
Schema.union([
Schema.object({
think: Schema.const(true).required(),
thinkModel: Schema.dynamic('model')
}),
Schema.object({})
]),
Schema.union([
Schema.object({
command: Schema.const(true).required(),
Expand Down
37 changes: 35 additions & 2 deletions packages/plugin-common/src/plugins/think.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
/* eslint-disable max-len */
import { Tool } from '@langchain/core/tools'
import { Context, Session } from 'koishi'
import { Context, Schema, Session } from 'koishi'
import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat'
import { Config } from '..'
import { ChatLunaChatModel } from 'koishi-plugin-chatluna/llm-core/platform/model'
import { PlatformService } from 'koishi-plugin-chatluna/llm-core/platform/service'
import { ModelType } from 'koishi-plugin-chatluna/llm-core/platform/types'
import { parseRawModelName } from 'koishi-plugin-chatluna/llm-core/utils/count_tokens'

export async function apply(
ctx: Context,
config: Config,
plugin: ChatLunaPlugin
) {
ctx.on('chatluna/model-added', (service) => {
ctx.schema.set('model', Schema.union(getModelNames(service)))
})

ctx.on('chatluna/model-removed', (service) => {
ctx.schema.set('model', Schema.union(getModelNames(service)))
})

ctx.schema.set('model', Schema.union(getModelNames(ctx.chatluna.platform)))

if (config.think === true) {
plugin.registerTool('think', {
selector(_) {
return true
},

async createTool(params, session) {
const thinkModel = config.thinkModel

if (thinkModel != null) {
const [platform, model] = parseRawModelName(thinkModel)
params.model = await ctx.chatluna.createChatModel(
platform,
model
)
}

return new ThinkTool(params.model)
}
})
Expand Down Expand Up @@ -45,6 +68,10 @@ export async function apply(
alwaysRecreate: true
})
}

function getModelNames(service: PlatformService) {
return service.getAllModels(ModelType.llm).map((m) => Schema.const(m))
}
}

export class ThinkTool extends Tool {
Expand Down Expand Up @@ -91,7 +118,13 @@ Proceed with executing this plan, using the suggested tools and your best judgme
try {
const thinkPrompt = this._thinkPrompt.replace('{input}', input)
const response = await this._model.invoke(thinkPrompt)
const analysis = response.content as string
let analysis = response.content as string

if (response.additional_kwargs?.reasoning_content) {
analysis = response.additional_kwargs[
'reasoning_content'
] as string
}

const finalResponse = this._responsePrompt.replace(
'{analysis}',
Expand Down

0 comments on commit 7a2b2d6

Please sign in to comment.