Skip to content

SMAT-Lab/CangGraph

Repository files navigation

用户教程

CangChain 是一个用于定义和管理 大模型调用 的专用语言。它允许开发人员通过结构化的系统提示词、工具和各类协作策略来增强大模型的功能。本手册将介绍如何使用 CangChain 的各种功能,并通过实例帮助用户快速上手。

CangChain 支持无提示词模板直接调用模型实例,下面将介绍调用方法。

目前,我们使用getLLMInstance()方法来定义创建一个大模型实例。

getLLMInstance(modelType: LLMType, model: String)

其中

属性名 值类型 说明
modelType LLMType 大模型类型选择;LLMType为枚举enum类型,可以从DeepSeekOllamaOpen_AI中选择,也可以根据需要扩充
model String 具体模型类型;根据不同厂商推出的模型,具体选择调用的模型

示例:

// 选择使用DeepSeek的deepseek-chat模型创建模型实例
llm = getLLMInstance(LLMType.DeepSeek, model: "deepseek-chat")

创建的模型实例可以直接通过query()方法调用。

query(content: String)

其中

属性名 值类型 说明
content String 请求的问题内容

示例:

llm.query("who are you?")

在调用大模型的过程中,仅凭借大模型自身的能力有时很难得到正确的答案。CangChain提供了工具调用接口,允许模型在给定的工具集中选择合适的工具调用来辅助回答问题。

CangChain的工具在构造过程中应当遵循一定的格式,工具格式参数如下:

Tool(
	toolType: String,
	functionArguments: ToolArgs
)
属性名 值类型 说明
toolType String 工具类型,通常来说为function
functionArguments ToolArgs 工具调用参数,定义工具名称、描述、返回格式等信息

其中function arguments构造格式如下:

ToolArgs(
    toolName: String,
    description: String,
    parameters: ToolParameters,
    required: Array<String>,
    additionalProperties: Bool
)
属性名 值类型 说明
toolName String 工具名,工具唯一标识
description String 工具描述,阐述工具的应用场景
parameters ToolParameters 工具返回参数,定义工具调用可选返回参数
required Array<String> 必要工具返回参数列表,定义工具调用必要返回参数,为工具返回参数子集
additionalProperties Bool 额外参数,通常为false

工具返回参数parameters也有特定构造方式:

ToolParameters(
	type: String,
	properties: HashMap<String, {propertyName: String, property: ToolProperty}>
	/*
	ToolProperty:
	propertyType: String
	description: String
	*/
)
属性名 值类型 说明
type String 返回参数类型,定义工具调用返回参数的类型,通常为object
properties HashMap<String, ToolProperty> 返回参数列表,阐述工具的应用场景
propertyName String 参数名称
propertyType String 参数类型
description String 参数描述

构造示例

let toolArgs = ToolArgs(
    /*toolName: */"translater",
    /*description: */"Translate Chinese to another language.",
    /*parameters: */
    ToolParameters(
        /*type: */"object",
        /*properties: */
        HashMap<String, ToolProperties>([
            (/*propertyName: */"text", ToolProperties(/*propertyType: */"string", /*description: */"text to translate")),
            (/*propertyName: */"target_language", ToolProperties(/*propertyType: */"string", /*description: */"language translated into"))
        ]),
        /*required: */
        ["text", "target_language"],
        /*additionalProperties: */
        false
    )
)

let translater: Tool = Tool(
    /*type:*/"function",
    /*functionArguments*/toolArgs
)

在构造好工具之后,可以直接在请求中添加工具列表,使用方法如下:

llm.query("Translate `你好,世界。` into English", [translater, calculator])

每个 Agent 的核心是系统提示词,它定义了 Agent 的角色信息和执行步骤,使得大语言模型能够更准确和快速地回答问题。在 Agent 定义中,利用Message序列编写 Agent 的系统提示词prompt。每个Agent最多只能有1个prompt。

CangChain中构建提示词的方式灵活,可以直接构建:

// 直接构建提示词
prompt = """
                You're a calculater.
                Calculate the x in the formula with detailed process.
                """

除此之外,也支持从外部文件导入:

// 从文件导入
prompt = getPromptFromFile("./promptFile.txt")

在构建好提示词之后,我们可以利用消息序列,将提示词与模型绑定构造Agent。

messages = ArrayList<Message>([Message("system", prompt)])

Agent的调用交互依赖于消息序列,调用Agent时的请求问题同样需要加入消息序列。

messages.append(Message("user", "Here is the formula: `x / 2 - 3x + 2 = 0`"))

在使用Agent调用交互时,需要传入包含提示词和请求问题的消息序列。

llm.query(messages.toArray())

此外,Agent的交互也允许添加工具调用。

llm.query(messages.toArray(), tools)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •