Additional Features
For the Qwen3 model, additional features are available that extend LLM functionality and provide more usage options. Usage examples may differ slightly from official OpenAI documentation and are provided below.
Tools
Getting a response from LLM in the form of a predefined data schema.
- Structured Output
- Function Calling
Пример для ChatOpenAI
from langchain_openai import ChatOpenAI
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
message = "Hello, I'm John and I'm 25 years old"
url = "http://localhost:5000/v1/"
client = ChatOpenAI(
model="Compressa-LLM",
api_key="TOKEN_1",
base_url=url,
)
struct_model = client.with_structured_output(User, method="function_calling")
response = struct_model.invoke([
{"role": "system", "content": "/think" },
{"role": "user", "content": message },
], tool_choice="auto", extra_body={"chat_template_kwargs": {"thinking": False}})
print(response.name, response.age)
Пример для ChatQwen
client = ChatQwen(
model="Compressa-LLM",
api_key="TOKEN_1",
api_base=url,
enable_thinking=False,
)
Использование LLM для получения структурированного ответа в виде функции и послед ующего выполнения функции с retry запросами к LLM в случае неуспеха.
from openai import OpenAI
import os
import sys
import io
def exec_user_code(code, test_cases):
local_vars = {}
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = io.StringIO()
sys.stderr = io.StringIO()
try:
exec(code, local_vars)
func_name = None
for name in local_vars:
if callable(local_vars[name]) and "palindrome" in name.lower():
func_name = name
break
assert func_name is not None, "No palindrome function found in generated code!"
results = []
for case in test_cases:
try:
result = local_vars[func_name](case)
results.append((case, result))
except Exception as e:
results.append((case, f"Exception: {e}"))
out = sys.stdout.getvalue()
err = sys.stderr.getvalue()
return {"stdout": out, "stderr": err, "results": results}
except Exception as e:
return {"stdout": sys.stdout.getvalue(), "stderr": sys.stderr.getvalue() + str(e), "results": []}
finally:
sys.stdout = old_stdout
sys.stderr = old_stderr
client = ChatOpenAI(
model="Compressa-LLM",
api_key="TOKEN_1",
base_url=url,
)
tools = [
{"type": "function",
"function": {
"name": "python_code",
"description": "Prepares python code for execution.",
"parameters": {
"type": "object",
"properties": {
"code": {"type": "string", "description": "Python code for execution"}
},
"required": ["code"]
}
}
}
]
user_request = "Write a function that checks if a string is a palindrome, and test it (not more than 3 examples)."
cases = ["madam", "", "a man a plan a canal panama", "banana"]
expected = [
("madam", True),
("", True),
("a man a plan a canal panama", True),
("banana", False),
]
messages = [{"role": "user", "content": user_request}]
MAX_ATTEMPTS = 3
attempt = 0
success = False
while attempt < MAX_ATTEMPTS and not success:
attempt += 1
print(f"\n========== TRY {attempt} ==========\n")
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
tool_choice="auto",
extra_body={"chat_template_kwargs": {"thinking": False}},
)
tool_call = response.choices[0].message.tool_calls[0]
user_code = args["code"]
result = exec_user_code(user_code, cases)
print("Execution result:\n")
print(result)
passed = True
for (input_str, expected_output), (tested_str, got_output) in zip(expected, result["results"]):
if got_output != expected_output:
print(f"Failed: {tested_str!r} -> {got_output!r} (expected {expected_output!r})")
passed = False
if passed:
print("All tests passed!")
success = True
break
else:
fail_report = "Your code did not pass the following tests:\n"
for (input_str, expected_output), (tested_str, got_output) in zip(expected, result["results"]):
if got_output != expected_output:
fail_report += f"Test case: {repr(tested_str)}. Got: {got_output!r}, expected: {expected_output!r}\n"
fail_report += "\nFix the code so that all tests pass."
messages.append(
{"role": "assistant", "content": None, "tool_calls": [tool_call.model_dump()]}
)
messages.append(
{"role": "tool", "tool_call_id": tool_call.id, "name": tool_call.function.name, "content": json.dumps(result)}
)
messages.append({"role": "user", "content": fail_report})
if not success:
print(f"\nModel {model} did not generate correct code after 3 attempts.")
Guidance
The official Guidance library as of fall 2025 does not support working with OpenAI-like clients. You can use the modified library from our repository.
pip uninstall guidance
pip install git+https://github.com/compressa-ai/compressa-guidance.git
pip show guidance
Version Version: 0.3.0rc0 should be displayed
Usage Examples
- Regex
- Select
Regular Expressions
email_generator = model(
model=compressa,
base_url=url,
api_key=key
)
with system():
email_generator += "You are a useful assistant. Don't use any other text than the answer."
with user():
email_generator += "Generate a valid email address."
with assistant():
email_generator += gen("lm_email", regex=r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", temperature=0.0)
Selection from List
quiz = model(
model=compressa,
base_url=url,
api_key=key
)
with system():
quiz += "You are a geography expert"
with user():
quiz += """What is the capital of Sweden? Answer with the correct letter only.
A) Helsinki
B) Reykjavík
C) Stockholm
D) Oslo
"""
with assistant():
quiz += select(["A", "B", "C", "D"], name="model_selection")
print(f"The model selected {quiz['model_selection']}")