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]>
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
from openai import OpenAI
|
|
|
|
client = OpenAI(api_key="foobar", base_url="http://localhost:1234/v1/")
|
|
|
|
BULLET_LIST_REGEX = "(- [^\n]*\n)+(- [^\n]*)(\n\n)?"
|
|
|
|
completion = client.chat.completions.create(
|
|
model="default",
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": "Write a list of jokes. Return a markdown list where each item is a joke.",
|
|
}
|
|
],
|
|
max_tokens=256,
|
|
frequency_penalty=1.0,
|
|
top_p=0.1,
|
|
temperature=0,
|
|
extra_body={"grammar": {"type": "regex", "value": BULLET_LIST_REGEX}},
|
|
)
|
|
|
|
print(completion.choices[0].message.content)
|
|
|
|
print("---")
|
|
|
|
# The following does token healing. Prompting the model to continue after a space usually breaks
|
|
# the text because the model wants to start the new token with a space. By setting the a space after
|
|
# "Sure!" we guarantee a space after "Sure!" but we haven't forced which token that starts with space should be used yet.
|
|
|
|
completion = client.chat.completions.create(
|
|
model="default",
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": "Tell me a joke.",
|
|
}
|
|
],
|
|
max_tokens=256,
|
|
frequency_penalty=1.0,
|
|
top_p=0.1,
|
|
temperature=0,
|
|
extra_body={"grammar": {"type": "regex", "value": "Sure! (?s:.)*"}},
|
|
)
|
|
|
|
print(completion.choices[0].message.content)
|