mirror of
https://github.com/zenlm/engine.git
synced 2026-10-30 15:00:05 +00:00
* Add model * Update loader * Fix llama config usage * Docs * Fix config no_rope_layers * Fix tie_word_embeddings default * Add chat template * Embed the chat templates * Fix embedding template * enable_thinking default true * Update examples * XML tools for smollm3 * Add smollm3 docs * Fix openai examples * Clippy --------- Co-authored-by: Eric Buehler <[email protected]>
65 lines
1.4 KiB
Python
65 lines
1.4 KiB
Python
from openai import OpenAI
|
|
|
|
client = OpenAI(api_key="foobar", base_url="http://localhost:1234/v1/")
|
|
|
|
c_lark = r"""
|
|
# start: program
|
|
# we generate only one
|
|
start: function_def
|
|
|
|
program: (function_def)*
|
|
|
|
function_def: type IDENTIFIER "(" params ")" "{" stmt* "}"
|
|
params: param ("," param)*
|
|
|
|
|
param: type IDENTIFIER
|
|
type: "int" | "float" | "char" | "void"
|
|
|
|
stmt: var_decl
|
|
| expr_stmt
|
|
| return_stmt
|
|
| if_stmt
|
|
| while_stmt
|
|
| block
|
|
|
|
var_decl: type IDENTIFIER ("=" expr)? ";"
|
|
expr_stmt: expr ";"
|
|
return_stmt: "return" expr ";"
|
|
if_stmt: "if" "(" expr ")" stmt ("else" stmt)?
|
|
while_stmt: "while" "(" expr ")" stmt
|
|
block: "{" stmt* "}"
|
|
|
|
expr: expr ("+" | "-" | "*" | "/" | "==" | "!=" | "<" | ">" | "<=" | ">=") expr
|
|
| "(" expr ")"
|
|
| IDENTIFIER
|
|
| NUMBER
|
|
| STRING
|
|
| IDENTIFIER "(" args ")"
|
|
args: expr ("," expr)*
|
|
|
|
|
|
|
IDENTIFIER: /[a-zA-Z_][a-zA-Z_0-9]*/
|
|
NUMBER: /[0-9]+(\.[0-9]+)?/
|
|
STRING: /"[^"]*"/
|
|
|
|
%import common.WS
|
|
%ignore WS
|
|
"""
|
|
|
|
completion = client.chat.completions.create(
|
|
model="default",
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": "Write the main function in C, returning 42. Answer with just the code, no explanation.",
|
|
}
|
|
],
|
|
max_tokens=256,
|
|
frequency_penalty=1.0,
|
|
top_p=0.1,
|
|
temperature=0,
|
|
extra_body={"grammar": {"type": "lark", "value": c_lark}},
|
|
)
|
|
|
|
print(completion.choices[0].message.content)
|