Skip to content

Commit

Permalink
BUG: Compat with openai extra body (#2759)
Browse files Browse the repository at this point in the history
  • Loading branch information
codingl2k1 authored Jan 13, 2025
1 parent df45f11 commit 7c6249a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
49 changes: 47 additions & 2 deletions xinference/core/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ def test_create_completion_types():
with pytest.raises(ValidationError):
CreateCompletion()

with pytest.raises(ValidationError):
CreateCompletion(model="abc", prompt="def", not_exist="jdk")
# In order to support extra body for openai client, we allow non exist key values.
# with pytest.raises(ValidationError):
# CreateCompletion(model="abc", prompt="def", not_exist="jdk")

CreateCompletion(model="abc", prompt="def")

Expand Down Expand Up @@ -88,3 +89,47 @@ def test_create_chat_completion_types():
# These chat and generate share the same type.
assert CreateChatCompletionLlamaCpp is CreateCompletionLlamaCpp
assert CreateChatCompletionTorch is CreateCompletionTorch


def test_openai_requests():
# https://github.com/xorbitsai/inference/pull/2673
# The following request body is generated by this:
# client = openai.Client(api_key="not empty", base_url="http://127.0.0.1:9997/v1")
#
# class TSchema(BaseModel):
# test: str
# num: int
#
# schema = TSchema.model_json_schema()
# messages = [
# {"role": "system", "content": "you are a helpful assistant"},
# {"role": "user", "content": "give me an example json, fill a random number"}
# ]
# completion = client.chat.completions.create(
# model="qwen2-instruct",
# messages=messages,
# extra_body={
# "guided_json": schema,
# "guided_decoding_backend": "outlines"
# })
obj = {
"messages": [
{"role": "system", "content": "you are a helpful assistant"},
{
"role": "user",
"content": "give me an example json, fill a random number",
},
],
"model": "qwen2-instruct",
"guided_json": {
"properties": {
"test": {"title": "Test", "type": "string"},
"num": {"title": "Num", "type": "integer"},
},
"required": ["test", "num"],
"title": "TSchema",
"type": "object",
},
"guided_decoding_backend": "outlines",
}
CreateChatCompletion.parse_obj(obj)
2 changes: 2 additions & 0 deletions xinference/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ def get_pydantic_model_from_method(
exclude_fields: Optional[Iterable[str]] = None,
include_fields: Optional[Dict[str, Any]] = None,
) -> BaseModel:
# The validate_arguments set Config.extra = "forbid" by default.
f = validate_arguments(meth, config={"arbitrary_types_allowed": True})
model = f.model
model.Config.extra = "ignore"
model.__fields__.pop("self", None)
model.__fields__.pop("args", None)
model.__fields__.pop("kwargs", None)
Expand Down

0 comments on commit 7c6249a

Please sign in to comment.