(function_call)
| 2073 | ) # type: ignore |
| 2074 | |
| 2075 | def get_grammar(function_call): |
| 2076 | function_body = None |
| 2077 | for function in functions or []: |
| 2078 | if function["name"] == function_call: |
| 2079 | function_body = function["parameters"] |
| 2080 | break |
| 2081 | for tool in tools or []: |
| 2082 | if tool["type"] == "function" and tool["function"]["name"] == function_call: |
| 2083 | function_body = tool["function"]["parameters"] |
| 2084 | break |
| 2085 | |
| 2086 | try: |
| 2087 | with suppress_stdout_stderr(disable=llama.verbose): |
| 2088 | grammar_text = llama_grammar.json_schema_to_gbnf( |
| 2089 | json.dumps(function_body) |
| 2090 | ) |
| 2091 | grammar = llama_grammar.LlamaGrammar.from_string( |
| 2092 | llama_grammar.json_schema_to_gbnf(json.dumps(function_body)) |
| 2093 | ) |
| 2094 | print(grammar_text) |
| 2095 | except Exception as e: |
| 2096 | if llama.verbose: |
| 2097 | print( |
| 2098 | "Failed to parse function body as JSON schema, falling back to default grammar" |
| 2099 | ) |
| 2100 | print(e) |
| 2101 | with suppress_stdout_stderr(disable=llama.verbose): |
| 2102 | grammar = llama_grammar.LlamaGrammar.from_string( |
| 2103 | llama_grammar.JSON_GBNF, verbose=llama.verbose |
| 2104 | ) |
| 2105 | |
| 2106 | return grammar |
| 2107 | |
| 2108 | def create_completion(prompt, stop, grammar): |
| 2109 | completion = cast( |
no test coverage detected
searching dependent graphs…