Skip to content

大语言模型

ChatLuna 的基本能力之一就是聚合和调用各种大语言模型。

基本用法

使用 chatluna 服务中的 createChatModel 方法来创建一个 ComputedRef<ChatLunaChatModel> 实例。

ts
import type {} from "koishi-plugin-chatluna/services/chat";

const 
modelRef
= await
ctx
.
chatluna
.
createChatModel
("openai/gpt-5-nano")
const
message
= await
modelRef
.
value
?.
invoke
("你好,世界!")
console
.
log
(
message
)

提示

自 ChatLuna 1.3.0 开始,ChatLuna 开始深度整合 vue 的响应式系统。如果返回的值为 ComputedRef<T>,则代表此值是可以根据其他配置改动进行变化的。

请先创建这个值并保存到类或者其他地方里面,在需要的时候调用 ref.value 来获取真正的值。 [!WARNING] 警告 响应式获取的 value 可能会 undefined。 如果返回空值,则说明当前获取的模型或者其他值不存在。你需要提取判断并告知用户,需要的模型或者其他参数不存在。

ChatLunaChatModel 继承自 BaseChatModel

你可以直接使用 BaseChatModel 的所有方法,并和 LangChain 的其他 API 无缝衔接。

传入消息列表与多模态

ChatLunaChatModel 也可以传入消息列表:

ts
import type {} from "koishi-plugin-chatluna/services/chat"
import { 
HumanMessage
,
SystemMessage
} from "@langchain/core/messages"
const
modelRef
= await
ctx
.
chatluna
.
createChatModel
("openai/gpt-5-nano")
const
messages
= [
new
SystemMessage
("You are a helpful assistant."),
new
HumanMessage
("你好,世界!")
] const
message
= await
modelRef
.
value
?.
invoke
(
messages
)
console
.
log
(
message
)

并且支持多模态消息(目前仅支持图片,还需确保使用的模型支持多模态输入):

ts
import type {} from "koishi-plugin-chatluna/services/chat"
import { 
HumanMessage
,
SystemMessage
} from "@langchain/core/messages"
const
modelRef
= await
ctx
.
chatluna
.
createChatModel
("openai/gpt-5-nano")
const
messages
= [
new
SystemMessage
("You are a helpful assistant."),
new
HumanMessage
({
content
: [
{
type
: "image_url",
image_url
: {
// 也支持 base64 编码的图片
url
: "https://example.com/image.png"
} }, {
type
: "text",
content
: "图片上的内容是什么?"
} ] }) ] const
message
= await
modelRef
.
value
?.
invoke
(
messages
)
console
.
log
(
message
)

获取可用的模型

在 ChatLuna 中,实际掌握各类模型和平台的是 PlatformService 类。

如果你需要获取全部可用的语言模型列表,可以调用 PlatformServicelistAllModels 方法。

ts
import type {} from "koishi-plugin-chatluna/services/chat";
import { 
ModelType
} from 'koishi-plugin-chatluna/llm-core/platform/types'
const
modelsRef
=
ctx
.
chatluna
.
platform
.
listAllModels
(
ModelType
.
llm
)
const
models
=
modelsRef
.
value

ModelType 是一个枚举类型,定义了模型类型:

ts
type 
PureModelType
=
Prettify
<typeof
ModelType
>







目前支持的模型类型有:

  • llm:大语言模型
  • embedding:嵌入模型
  • all:所有模型

获取指定模型的信息

listAllModels 方法返回的是一系列模型信息数组。

我们可以获取指定模型的信息。

ts
const 
modelInfo
=
ctx
.
chatluna
.
platform
.
findModel
("openai/gpt-5-nano")
type
ModelInfo
=
Prettify
<typeof
modelInfo
.
value
>







返回的是一个 ModelInfo 对象,包含了模型的详细信息。具体的定义如下:

ts
interface ModelInfo {
    // 模型名称
    
name
: string
// 模型类型
type
:
ModelType
// 模型的上下文大小
maxTokens
: number
// 模型支持的能力 (多模态,工具调用等)
capabilities
:
ModelCapabilities
[]
} type
PureCapabilities
=
Prettify
<typeof
ModelCapabilities
>

页面历史

在 CC-BY-SA-4.0 许可下发布