Skip to content

Commit

Permalink
Update input_to_string
Browse files Browse the repository at this point in the history
Updated the method input_to_string to ensure compatibility with vision models.
  • Loading branch information
diogoazevedo15 committed Sep 2, 2024
1 parent 25e3353 commit 66bc3ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
2 changes: 1 addition & 1 deletion llmstudio/engine/providers/azure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ast # Add this import to safely evaluate string representations of lists/dicts
import ast
import asyncio
import json
import os
Expand Down
44 changes: 16 additions & 28 deletions llmstudio/engine/providers/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ async def chat(
if request.is_stream:
return StreamingResponse(response_handler)
else:
return JSONResponse(content=await response_handler.__anext__())
return JSONResponse(content= await response_handler.__anext__())
except HTTPException as e:
if e.status_code == 429:
continue # Retry on rate limit error
else:
raise e # Raise other HTTP exceptions
except Exception as e:
print(e)
raise HTTPException(
status_code=500, detail=str(e)
) # Raise other exceptions as HTTP 500
Expand Down Expand Up @@ -310,26 +311,6 @@ def join_chunks(self, chunks, request):
)
)

chunk = ChatCompletion(
id=chunks[-1].get("id"),
created=chunks[-1].get("created"),
model=chunks[-1].get("model"),
object="chat.completion",
choices=[
Choice(
finish_reason="stop",
index=0,
logprobs=None,
message=ChatCompletionMessage(
content=stop_content,
role="assistant",
function_call=None,
tool_calls=None,
),
)
],
)

return (
ChatCompletion(
id=chunks[-1].get("id"),
Expand Down Expand Up @@ -406,13 +387,20 @@ def input_to_string(self, input):
if isinstance(input, str):
return input
else:
return "".join(
[
message.get("content", "")
for message in input
if message.get("content") is not None
]
)
result = []
for message in input:
if message.get("content") is not None:
if isinstance(message["content"], str):
result.append(message["content"])
elif isinstance(message["content"], list) and message.get("role") == "user":
for item in message["content"]:
if item.get("type") == "text":
result.append(item.get("text", ""))
elif item.get("type") == "image_url":
url = item.get("image_url", {}).get("url", "")
result.append(url)
return "".join(result)


def output_to_string(self, output):
if output.choices[0].finish_reason == "stop":
Expand Down

0 comments on commit 66bc3ae

Please sign in to comment.