Дополнительные возможности
Для модели Qwen3 доступны дополнительные возможности, расширяющие функционал LLM и дающие больше возможностей для использования. Примеры использования могут несколько отличаться от официальной документации OpenAI и приведены ниже.
Structured Output
Получение ответа от LLM в виде заранее определенной схемы данных.
- Structured Output
- Structured Output
- Function Calling
- Regex
- Select
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,
)
Function Calling
Использование 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.")