[Bugfix] Add fix for JSON whitespace (#4189)

Co-authored-by: Ubuntu <ubuntu@ip-172-31-13-147.ec2.internal>
This commit is contained in:
Ayush Rautwar 2024-04-19 23:49:22 -04:00 committed by GitHub
parent bc9df1571b
commit 138485a82d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 12 deletions

View File

@ -754,19 +754,20 @@ async def test_guided_choice_chat_logprobs(server, client: openai.AsyncOpenAI,
async def test_response_format_json_object(server, client: openai.AsyncOpenAI): async def test_response_format_json_object(server, client: openai.AsyncOpenAI):
resp = await client.chat.completions.create( for _ in range(2):
model=MODEL_NAME, resp = await client.chat.completions.create(
messages=[{ model=MODEL_NAME,
"role": messages=[{
"user", "role":
"content": ('what is 1+1? please respond with a JSON object, ' "user",
'the format is {"result": 2}') "content": ('what is 1+1? please respond with a JSON object, '
}], 'the format is {"result": 2}')
response_format={"type": "json_object"}) }],
response_format={"type": "json_object"})
content = resp.choices[0].message.content content = resp.choices[0].message.content
loaded = json.loads(content) loaded = json.loads(content)
assert loaded == {"result": 2}, loaded assert loaded == {"result": 2}, loaded
async def test_guided_grammar(server, client: openai.AsyncOpenAI): async def test_guided_grammar(server, client: openai.AsyncOpenAI):

View File

@ -131,6 +131,11 @@ class CFGLogitsProcessor(BaseLogitsProcessor):
fsm = CFGFSM(cfg, tokenizer) fsm = CFGFSM(cfg, tokenizer)
self.fsm = fsm self.fsm = fsm
def init_state(self):
"""Initialize state with a CFGFSM copy."""
super().init_state()
self.fsm = self.fsm.copy()
@lru_cache @lru_cache
def _adapt_tokenizer(tokenizer: PreTrainedTokenizerBase): def _adapt_tokenizer(tokenizer: PreTrainedTokenizerBase):