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

WIP: Refactor generate client to include usage from providers #197

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
44 changes: 26 additions & 18 deletions examples/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
from pprint import pprint
import os
from dotenv import load_dotenv
import asyncio
load_dotenv()

def run_provider(provider, model, api_key, **kwargs):
llm = LLMCore(provider=provider, api_key=api_key, **kwargs)

llm = LLMCore(provider=provider, api_key=api_key, **kwargs)
latencies = {}

"""
chat_request = build_chat_request(model, chat_input="Hello, my name is Jason Json", is_stream=False)

import asyncio
response_async = asyncio.run(llm.achat(**chat_request))
pprint(response_async)
latencies["async (ms)"]= response_async.metrics["latency_s"]*1000
"""

# stream
print("\nasync stream")
Expand Down Expand Up @@ -114,6 +117,7 @@ def build_chat_request(model: str, chat_input: str, is_stream: bool, max_tokens:
# pprint(latencies)
# # we need credits

"""
provider = "azure"
model = "gpt-4o-mini"
for _ in range(1):
Expand All @@ -122,24 +126,28 @@ def build_chat_request(model: str, chat_input: str, is_stream: bool, max_tokens:
api_version=os.environ["AZURE_API_VERSION"],
api_endpoint=os.environ["AZURE_API_ENDPOINT"])
pprint(latencies)
"""

provider = "azure"
model = "o1-preview"
for _ in range(1):
latencies = run_provider(provider=provider, model=model,
api_key=os.environ["AZURE_API_KEY"],
api_version=os.environ["AZURE_API_VERSION"],
api_endpoint=os.environ["AZURE_API_ENDPOINT"])
pprint(latencies)
# provider = "azure"
# model = "o1-preview"
# for _ in range(1):
# latencies = run_provider(provider=provider, model=model,
# api_key=os.environ["AZURE_API_KEY"],
# api_version=os.environ["AZURE_API_VERSION"],
# api_endpoint=os.environ["AZURE_API_ENDPOINT"])
# pprint(latencies)
# # we need a deployment


provider = "azure"
model = "o1-mini"
for _ in range(1):
latencies = run_provider(provider=provider, model=model,
api_key=os.environ["AZURE_API_KEY"],
api_version=os.environ["AZURE_API_VERSION"],
api_endpoint=os.environ["AZURE_API_ENDPOINT"])
pprint(latencies)
# provider = "azure"
# model = "o1-mini"
# for _ in range(1):
# latencies = run_provider(provider=provider, model=model,
# api_key=os.environ["AZURE_API_KEY"],
# api_version=os.environ["AZURE_API_VERSION"],
# api_endpoint=os.environ["AZURE_API_ENDPOINT"])
# pprint(latencies)
# # we need a deployment

# provider = "azure"
# model = "gpt-4o"
Expand Down
8 changes: 4 additions & 4 deletions libs/core/llmstudio_core/providers/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def generate_client(self, request: ChatRequest) -> Any:
base_args = {
"model": request.model,
"messages": messages,
"stream": True,
"stream": request.is_stream,
}

combined_args = {
Expand Down Expand Up @@ -145,14 +145,14 @@ def prepare_messages(self, request: ChatRequest):
else request.chat_input
)

async def aparse_response(
async def _aparse_response(
self, response: AsyncGenerator, **kwargs
) -> AsyncGenerator[str, None]:
result = self.parse_response(response=response, **kwargs)
result = self._parse_response(response=response, **kwargs)
for chunk in result:
yield chunk

def parse_response(self, response: AsyncGenerator, **kwargs) -> Any:
def _parse_response(self, response: AsyncGenerator, **kwargs) -> Any:
"""
Processes a generator response and yields processed chunks.

Expand Down
6 changes: 3 additions & 3 deletions libs/core/llmstudio_core/providers/bedrock/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ def generate_client(self, request: ChatRequest) -> Coroutine[Any, Any, Generator
except Exception as e:
raise ProviderError(str(e))

async def aparse_response(
async def _aparse_response(
self, response: Any, **kwargs
) -> AsyncGenerator[Any, None]:
return self.parse_response(response=response, **kwargs)
return self._parse_response(response=response, **kwargs)

def parse_response(self, response: AsyncGenerator[Any, None], **kwargs) -> Any:
def _parse_response(self, response: AsyncGenerator[Any, None], **kwargs) -> Any:
tool_name = None
tool_arguments = ""
tool_id = None
Expand Down
8 changes: 4 additions & 4 deletions libs/core/llmstudio_core/providers/bedrock/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def generate_client(self, request: ChatRequest) -> Coroutine[Any, Any, Generator
self.selected_model = self._get_provider(request.model)
return self.selected_model.generate_client(request=request)

async def aparse_response(
async def _aparse_response(
self, response: Any, **kwargs
) -> AsyncGenerator[Any, None]:
result = await self.selected_model.aparse_response(response=response, **kwargs)
result = await self.selected_model._aparse_response(response=response, **kwargs)
for chunk in result:
yield chunk

def parse_response(self, response: AsyncGenerator[Any, None], **kwargs) -> Any:
return self.selected_model.parse_response(response=response, **kwargs)
def _parse_response(self, response: AsyncGenerator[Any, None], **kwargs) -> Any:
return self.selected_model._parse_response(response=response, **kwargs)
8 changes: 4 additions & 4 deletions libs/core/llmstudio_core/providers/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ def generate_client(self, request: ChatRequest) -> Generator:
if isinstance(request.chat_input, str)
else request.chat_input
),
stream=True,
stream=request.is_stream,
**request.parameters,
)
except openai._exceptions.APIError as e:
raise ProviderError(str(e))

async def aparse_response(
async def _aparse_response(
self, response: AsyncGenerator, **kwargs
) -> AsyncGenerator[str, None]:
result = self.parse_response(response=response, **kwargs)
result = self._parse_response(response=response, **kwargs)
for chunk in result:
yield chunk

def parse_response(self, response: Generator, **kwargs) -> Generator:
def _parse_response(self, response: Generator, **kwargs) -> Generator:
for chunk in response:
yield chunk.model_dump()
Loading