Openapi 使用 JSON Schema 实现包含字典作为参数之一的函数调用

问题描述 投票:0回答:2

解释

我目前正在使用 openapi 和 python 开发一个游戏项目。下面给出了我编写的函数调用之一。

获取案例文件

{
    "name": "getCaseFile",
    "description": "Generate a hard to solve, self-contained detective game scenario including a brief introduction, setting, key characters, and initial evidence or clues. The scenario should be structured to allow for interactive investigation through text commands such as 'Inspect the area', 'Talk to [character name]', or 'Examine [object]'. Ask user whats their next move but don't display choices of what to do. Provide a clear mystery or objective for the player to solve, ensuring the narrative includes enough detail for engaging gameplay without prior knowledge of this conversation. Avoid making your message seem like a chatgpt response and make it so its like a story beginning, maybe your partner letting you in on the details or maybe you just got the files on the event and your reading them. AVOID TEXT FORMATTING since the text will be displayed in a terminal.",
    "parameters": {
        "type": "object",
        "required": ["case_name", "intro", "setting", "key_characters", "initial_clues", "solution", "objective"],
        "properties": {
            "case_name": {
                "type": "string",
                "description": "The name of the case file. Eg: The Vanishing Vintner"
            },
            "intro": {
                "type": "string",
                "description": "A brief introduction to the story. Eg: As a seasoned detective, you were just briefed on a new case by your partner, who looked specifically concerned about this one. 'It's baffling,' they said, handing over a file. 'An esteemed winemaker, Paulo Rossetti, vanished without a trace from his locked winery. Here's the kicker: no one was seen entering or leaving the place around the time he disappeared. We need your expertise.' You open the dossier and begin unraveling the case details."
            },
            "setting": {
                "type": "string",
                "description": "A brief explanation of the setting. Eg: The case is set at the Rossetti Vineyard, a sprawling estate known for its exquisite wines and its owner's exceptional taste. The majestic villa and adjacent winery are surrounded by acres of grapevines. Paulo Rossetti was last seen in his private study inside the winery, a room known to have one solid locked door and no windows."
            },
            "key_characters": {
                "type": "array",
                "description": "A list of suspects with their name, maybe title or position, and their relationship to the crime",
                "items": {
                    "type": "string"
                }
            },
            "initial_clues": {
                "type": "array",
                "description": "A list of initial clues. Eg: A vintage bottle of wine, with its seal intact, was left on Paulo's desk.",
                "items": {
                    "type": "string"
                }
            },
            "solution": {
                "type": "object",
                "additionalProperties": {
                    "type": "object",
                    "required" : ["culprit", "motive", "key_evidence"],
                    "properties": {
                        "culprit": {
                            "type": "string",
                            "description": "Name and last name of the culprit."
                        },
                        "motive" : {
                            "type": "string",
                            "description": "Motive of the culprit."
                        },
                        "key_evidence": {
                            "type": "string",
                            "description": "The key evidence for the crime commited by the culprit."
                        }
                    }
                }
            },
            "objective": {
                "type": "string",
                "description": "A simple objective to let player know that its time to act. Eg: Determine who is responsible for Paulo Rossetti's disappearance and uncover the motive behind their actions."
            }
        }
    }

}

如您所见,该函数需要 7 个参数,其中一个是

solution
,一个内部有 3 个键的字典。

当我使用此函数调用 OpenApi Playground 时,它没有为我提供

solution

下面给出一个例子:

getCaseFile(
    {
     "case_name": "The Mysterious Machinations at Merriweather Mansion", 
     "intro": "Just as you settle into your office with a steamy cup of coffee, your partner slides a bulky folder across the desk toward you. 'Here's something that will put that caffeine to good use,' they quip. The file contains details about the passing of a reclusive millionaire, Bernard Blackwood, found motionless at his estate. 'Bernard was a hermit, hardly seen outside his study. The doors were locked from the inside. No signs of forced entry, no obvious cause of death. This case is a real head-scratcher.'", 
     "setting": "The sprawling and enigmatic Merriweather Mansion, a remote abode located just outside the sleepy town of Galeshire. The mansion is known for its eccentric design and assortment of automatons and contraptions. Bernard was discovered in his personal study, a place brimming with oddities and inventions.", 
     "key_characters": [
          "Bernard Blackwood - The deceased millionaire and inventor.", 
          "Cecil Hawthorne - The estate's caretaker and Bernard's old friend.", 
          "Agatha Christie - The suspiciously named and ambitious niece.", 
          "Dr. Edward Bellamy - The family doctor last to visit Bernard.", 
          "Sofia Marlowe - The reclusive author living as a guest on the property."
     ], 
     "initial_clues": [
          "An unusual mechanical device on Bernard's desk, unfinished and whirring silently.", 
          "A will, freshly revised, with significant changes to beneficiaries.", 
          "A torn piece of fabric caught on a bramble outside a study window that doesn't quite close.", 
          "Bernard's journal, open to a page expressing his growing paranoia over someone betraying him."
     ], 
     "objective": "Unravel the cause of Bernard's death, decipher who could have engineered such an elaborate act, and determine their motive."
     }
 )

出现这种情况的原因是什么,如何解决?

python json openapi jsonschema
2个回答
0
投票

我无法解释为什么该工具省略了

solution
属性,但架构看起来不正确。

按照您的方式,它是一个可以采用任意名称的任意数量的属性的对象,其中每个值本身必须是具有

culprit
motive
key_evidence
属性的对象。从您的描述来看,这听起来嵌套的一层太深了。

我解决此问题的建议是提取内部模式并丢弃外部模式(使用

type
additionalProperties
)。


0
投票

经过长时间的网络搜索,我解决了我的问题。

显然

additionalProperties
关键字用于控制额外内容的处理,即名称未在
properties
关键字中列出或与
patternProperties
关键字中的任何正则表达式匹配的属性。

正确的用法是:

...

"solution": {
        "type": "object",
        "properties": {
             "culprit": {
                  "type": "string",
                  "description": "Name and last name of the culprit."
             },
             "motive" : {
                  "type": "string",
                  "description": "Motive of the culprit."
             },
             "key_evidence": {
                  "type": "string",
                  "description": "The key evidence for the crime commited by the culprit."
             }
         }
     }

...

我没有使用

additionalProperties
,而是使用了
properties
,它允许我拥有在架构上列出名称的属性。

© www.soinside.com 2019 - 2024. All rights reserved.