Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providers Refactor #175

Merged
merged 8 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 5 additions & 62 deletions libs/core/llmstudio_core/providers/azure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ast
import asyncio
import json
import os
import time
Expand Down Expand Up @@ -60,57 +59,7 @@ def validate_request(self, request: ChatRequest):

async def agenerate_client(self, request: ChatRequest) -> Any:
"""Generate an AzureOpenAI client"""

self.is_llama = "llama" in request.model.lower()
self.is_openai = "gpt" in request.model.lower()
self.has_tools = request.parameters.get("tools") is not None
self.has_functions = request.parameters.get("functions") is not None

try:
messages = self.prepare_messages(request)

# Prepare the optional tool-related arguments
tool_args = {}
if not self.is_llama and self.has_tools and self.is_openai:
tool_args = {
"tools": request.parameters.get("tools"),
"tool_choice": "auto" if request.parameters.get("tools") else None,
}

# Prepare the optional function-related arguments
function_args = {}
if not self.is_llama and self.has_functions and self.is_openai:
function_args = {
"functions": request.parameters.get("functions"),
"function_call": "auto"
if request.parameters.get("functions")
else None,
}

# Prepare the base arguments
base_args = {
"model": request.model,
"messages": messages,
"stream": True,
}

# Combine all arguments
combined_args = {
**base_args,
**tool_args,
**function_args,
**request.parameters,
}
# Perform the asynchronous call
return await asyncio.to_thread(
self._client.chat.completions.create, **combined_args
)

except openai._exceptions.APIConnectionError as e:
raise ProviderError(f"There was an error reaching the endpoint: {e}")

except openai._exceptions.APIStatusError as e:
raise ProviderError(e.response.json())
return self.generate_client(request=request)

def generate_client(self, request: ChatRequest) -> Any:
"""Generate an AzureOpenAI client"""
Expand Down Expand Up @@ -185,17 +134,11 @@ def prepare_messages(self, request: ChatRequest):
async def aparse_response(
self, response: AsyncGenerator, **kwargs
) -> AsyncGenerator[str, None]:
if self.is_llama and (self.has_tools or self.has_functions):
for chunk in self.handle_tool_response(response, **kwargs):
if chunk:
yield chunk
else:
for chunk in response:
c = chunk.model_dump()
if c.get("choices"):
yield c
result = self.parse_response(response=response, **kwargs)
for chunk in result:
yield chunk

def parse_response(self, response: Generator, **kwargs) -> Any:
def parse_response(self, response: AsyncGenerator, **kwargs) -> Any:
if self.is_llama and (self.has_tools or self.has_functions):
for chunk in self.handle_tool_response(response, **kwargs):
if chunk:
Expand Down
22 changes: 4 additions & 18 deletions libs/core/llmstudio_core/providers/openai.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import os
from typing import Any, AsyncGenerator, Coroutine, Generator

Expand Down Expand Up @@ -26,21 +25,7 @@ async def agenerate_client(
self, request: ChatRequest
) -> Coroutine[Any, Any, Generator]:
"""Generate an OpenAI client"""

try:
return await asyncio.to_thread(
self._client.chat.completions.create,
model=request.model,
messages=(
[{"role": "user", "content": request.chat_input}]
if isinstance(request.chat_input, str)
else request.chat_input
),
stream=True,
**request.parameters,
)
except openai._exceptions.APIError as e:
raise ProviderError(str(e))
return self.generate_client(request=request)

def generate_client(self, request: ChatRequest) -> Generator:
"""Generate an OpenAI client"""
Expand All @@ -62,8 +47,9 @@ def generate_client(self, request: ChatRequest) -> Generator:
async def aparse_response(
self, response: AsyncGenerator, **kwargs
) -> AsyncGenerator[str, None]:
for chunk in response:
yield chunk.model_dump()
result = self.parse_response(response=response, **kwargs)
for chunk in result:
yield chunk

def parse_response(self, response: Generator, **kwargs) -> Generator:
for chunk in response:
Expand Down
Loading