已把工具调用从 XML 改成 OpenAI 兼容 JSON,并统一解析/执行流程。改动概览如下:
新增 JSON tool_calls 解析/序列化并替换核心执行与提示词为 JSON-only:JsonToolCallParser.cs、AIIntelligenceCore.cs 工具基类移除 XML 解析,统一 JSON 参数读取与类型转换辅助:AITool.cs 工具实现统一 JSON args/UsageSchema(含重写/修复):Tool_ModifyGoodwill.cs、Tool_SendReinforcement.cs、Tool_GetMapPawns.cs、Tool_GetMapResources.cs、Tool_GetAvailablePrefabs.cs、Tool_CallPrefabAirdrop.cs、Tool_CallBombardment.cs、Tool_GetAvailableBombardments.cs、Tool_GetPawnStatus.cs、Tool_GetRecentNotifications.cs、Tool_SearchThingDef.cs、Tool_SearchPawnKind.cs、Tool_ChangeExpression.cs、Tool_SetOverwatchMode.cs、Tool_RememberFact.cs、Tool_RecallMemories.cs、Tool_SpawnResources.cs、Tool_AnalyzeScreen.cs 轰炸相关解析统一到 JSON 字典并增强数值解析:BombardmentUtility.cs UI 对话展示改为剥离 JSON tool_calls:Overlay_WulaLink.cs、Dialog_AIConversation.cs
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
JSON Output
|
||||
In many scenarios, users need the model to output in strict JSON format to achieve structured output, facilitating subsequent parsing.
|
||||
|
||||
DeepSeek provides JSON Output to ensure the model outputs valid JSON strings.
|
||||
|
||||
Notice
|
||||
To enable JSON Output, users should:
|
||||
|
||||
Set the response_format parameter to {'type': 'json_object'}.
|
||||
Include the word "json" in the system or user prompt, and provide an example of the desired JSON format to guide the model in outputting valid JSON.
|
||||
Set the max_tokens parameter reasonably to prevent the JSON string from being truncated midway.
|
||||
When using the JSON Output feature, the API may occasionally return empty content. We are actively working on optimizing this issue. You can try modifying the prompt to mitigate such problems.
|
||||
Sample Code
|
||||
Here is the complete Python code demonstrating the use of JSON Output:
|
||||
|
||||
import json
|
||||
from openai import OpenAI
|
||||
|
||||
client = OpenAI(
|
||||
api_key="<your api key>",
|
||||
base_url="https://api.deepseek.com",
|
||||
)
|
||||
|
||||
system_prompt = """
|
||||
The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format.
|
||||
|
||||
EXAMPLE INPUT:
|
||||
Which is the highest mountain in the world? Mount Everest.
|
||||
|
||||
EXAMPLE JSON OUTPUT:
|
||||
{
|
||||
"question": "Which is the highest mountain in the world?",
|
||||
"answer": "Mount Everest"
|
||||
}
|
||||
"""
|
||||
|
||||
user_prompt = "Which is the longest river in the world? The Nile River."
|
||||
|
||||
messages = [{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt}]
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=messages,
|
||||
response_format={
|
||||
'type': 'json_object'
|
||||
}
|
||||
)
|
||||
|
||||
print(json.loads(response.choices[0].message.content))
|
||||
|
||||
|
||||
The model will output:
|
||||
|
||||
{
|
||||
"question": "Which is the longest river in the world?",
|
||||
"answer": "The Nile River"
|
||||
}
|
||||
273
Source/WulaFallenEmpire/WulaAI_DevDocs/deepseek/ToolCalls.md
Normal file
273
Source/WulaFallenEmpire/WulaAI_DevDocs/deepseek/ToolCalls.md
Normal file
@@ -0,0 +1,273 @@
|
||||
Tool Calls
|
||||
Tool Calls allows the model to call external tools to enhance its capabilities.
|
||||
|
||||
Non-thinking Mode
|
||||
Sample Code
|
||||
Here is an example of using Tool Calls to get the current weather information of the user's location, demonstrated with complete Python code.
|
||||
|
||||
For the specific API format of Tool Calls, please refer to the Chat Completion documentation.
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
def send_messages(messages):
|
||||
response = client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=messages,
|
||||
tools=tools
|
||||
)
|
||||
return response.choices[0].message
|
||||
|
||||
client = OpenAI(
|
||||
api_key="<your api key>",
|
||||
base_url="https://api.deepseek.com",
|
||||
)
|
||||
|
||||
tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"description": "Get weather of a location, the user should supply a location first.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {
|
||||
"type": "string",
|
||||
"description": "The city and state, e.g. San Francisco, CA",
|
||||
}
|
||||
},
|
||||
"required": ["location"]
|
||||
},
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
messages = [{"role": "user", "content": "How's the weather in Hangzhou, Zhejiang?"}]
|
||||
message = send_messages(messages)
|
||||
print(f"User>\t {messages[0]['content']}")
|
||||
|
||||
tool = message.tool_calls[0]
|
||||
messages.append(message)
|
||||
|
||||
messages.append({"role": "tool", "tool_call_id": tool.id, "content": "24℃"})
|
||||
message = send_messages(messages)
|
||||
print(f"Model>\t {message.content}")
|
||||
|
||||
The execution flow of this example is as follows:
|
||||
|
||||
User: Asks about the current weather in Hangzhou
|
||||
Model: Returns the function get_weather({location: 'Hangzhou'})
|
||||
User: Calls the function get_weather({location: 'Hangzhou'}) and provides the result to the model
|
||||
Model: Returns in natural language, "The current temperature in Hangzhou is 24°C."
|
||||
Note: In the above code, the functionality of the get_weather function needs to be provided by the user. The model itself does not execute specific functions.
|
||||
|
||||
Thinking Mode
|
||||
From DeepSeek-V3.2, the API supports tool use in the thinking mode. For more details, please refer to Thinking Mode
|
||||
|
||||
strict Mode (Beta)
|
||||
In strict mode, the model strictly adheres to the format requirements of the Function's JSON schema when outputting a tool call, ensuring that the model's output complies with the user's definition. It is supported by both thinking and non-thinking mode.
|
||||
|
||||
To use strict mode, you need to::
|
||||
|
||||
Use base_url="https://api.deepseek.com/beta" to enable Beta features
|
||||
In the tools parameter,all function need to set the strict property to true
|
||||
The server will validate the JSON Schema of the Function provided by the user. If the schema does not conform to the specifications or contains JSON schema types that are not supported by the server, an error message will be returned
|
||||
The following is an example of a tool definition in the strict mode:
|
||||
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"strict": true,
|
||||
"description": "Get weather of a location, the user should supply a location first.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {
|
||||
"type": "string",
|
||||
"description": "The city and state, e.g. San Francisco, CA",
|
||||
}
|
||||
},
|
||||
"required": ["location"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Support Json Schema Types In strict Mode
|
||||
object
|
||||
string
|
||||
number
|
||||
integer
|
||||
boolean
|
||||
array
|
||||
enum
|
||||
anyOf
|
||||
object
|
||||
The object defines a nested structure containing key-value pairs, where properties specifies the schema for each key (or property) within the object. All properties of every object must be set as required, and the additionalProperties attribute of the object must be set to false.
|
||||
|
||||
Example:
|
||||
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": { "type": "string" },
|
||||
"age": { "type": "integer" }
|
||||
},
|
||||
"required": ["name", "age"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
|
||||
string
|
||||
Supported parameters:
|
||||
|
||||
pattern: Uses regular expressions to constrain the format of the string
|
||||
format: Validates the string against predefined common formats. Currently supported formats:
|
||||
email: Email address
|
||||
hostname: Hostname
|
||||
ipv4: IPv4 address
|
||||
ipv6: IPv6 address
|
||||
uuid: UUID
|
||||
Unsupported parameters:
|
||||
|
||||
minLength
|
||||
maxLength
|
||||
Example:
|
||||
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"user_email": {
|
||||
"type": "string",
|
||||
"description": "The user's email address",
|
||||
"format": "email"
|
||||
},
|
||||
"zip_code": {
|
||||
"type": "string",
|
||||
"description": "Six digit postal code",
|
||||
"pattern": "^\\d{6}$"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
number/integer
|
||||
Supported parameters:
|
||||
const: Specifies a constant numeric value
|
||||
default: Defines the default value of the number
|
||||
minimum: Specifies the minimum value
|
||||
maximum: Specifies the maximum value
|
||||
exclusiveMinimum: Defines a value that the number must be greater than
|
||||
exclusiveMaximum: Defines a value that the number must be less than
|
||||
multipleOf: Ensures that the number is a multiple of the specified value
|
||||
Example:
|
||||
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"score": {
|
||||
"type": "integer",
|
||||
"description": "A number from 1-5, which represents your rating, the higher, the better",
|
||||
"minimum": 1,
|
||||
"maximum": 5
|
||||
}
|
||||
},
|
||||
"required": ["score"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
|
||||
array
|
||||
Unsupported parameters:
|
||||
minItems
|
||||
maxItems
|
||||
Example:
|
||||
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"keywords": {
|
||||
"type": "array",
|
||||
"description": "Five keywords of the article, sorted by importance",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"description": "A concise and accurate keyword or phrase."
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["keywords"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
|
||||
enum
|
||||
The enum ensures that the output is one of the predefined options. For example, in the case of order status, it can only be one of a limited set of specified states.
|
||||
|
||||
Example:
|
||||
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"order_status": {
|
||||
"type": "string",
|
||||
"description": "Ordering status",
|
||||
"enum": ["pending", "processing", "shipped", "cancelled"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
anyOf
|
||||
Matches any one of the provided schemas, allowing fields to accommodate multiple valid formats. For example, a user's account could be either an email address or a phone number:
|
||||
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"account": {
|
||||
"anyOf": [
|
||||
{ "type": "string", "format": "email", "description": "可以是电子邮件地址" },
|
||||
{ "type": "string", "pattern": "^\\d{11}$", "description": "或11位手机号码" }
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$ref and $def
|
||||
You can use $def to define reusable modules and then use $ref to reference them, reducing schema repetition and enabling modularization. Additionally, $ref can be used independently to define recursive structures.
|
||||
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"report_date": {
|
||||
"type": "string",
|
||||
"description": "The date when the report was published"
|
||||
},
|
||||
"authors": {
|
||||
"type": "array",
|
||||
"description": "The authors of the report",
|
||||
"items": {
|
||||
"$ref": "#/$def/author"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["report_date", "authors"],
|
||||
"additionalProperties": false,
|
||||
"$def": {
|
||||
"authors": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"description": "author's name"
|
||||
},
|
||||
"institution": {
|
||||
"type": "string",
|
||||
"description": "author's institution"
|
||||
},
|
||||
"email": {
|
||||
"type": "string",
|
||||
"format": "email",
|
||||
"description": "author's email"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"required": ["name", "institution", "email"]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user