-
Notifications
You must be signed in to change notification settings - Fork 118
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
Added azureai client #36
Open
VindyaKonjarla
wants to merge
6
commits into
ray-project:main
Choose a base branch
from
VindyaKonjarla:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
85cb468
Refactor common.py file
3b6ada1
Create AzureAI client file for the azure pdeployed api
06a0833
Add readme file
9ed5695
Chance the readme.md file
036ff0a
Update README.md
VindyaKonjarla 1ba6d22
Update azureai_chat_completion.py
SameepPanigrahi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import json | ||
import os | ||
import time | ||
from typing import Any, Dict | ||
|
||
import ray | ||
import requests | ||
|
||
from llmperf.ray_llm_client import LLMClient | ||
from llmperf.models import RequestConfig | ||
from llmperf import common_metrics | ||
|
||
@ray.remote | ||
class AzureAIChatCompletionsClient(LLMClient): | ||
"""Client for AzureAI Chat Completions API.""" | ||
|
||
def llm_request(self, request_config: RequestConfig) -> Dict[str, Any]: | ||
prompt = request_config.prompt | ||
prompt, prompt_len = prompt | ||
|
||
message = [ | ||
{"role": "system", "content": ""}, | ||
{"role": "user", "content": prompt}, | ||
] | ||
model = request_config.model | ||
body = { | ||
"model": model, | ||
"messages": message, | ||
"stream": True, | ||
} | ||
sampling_params = request_config.sampling_params | ||
body.update(sampling_params or {}) | ||
time_to_next_token = [] | ||
tokens_received = 0 | ||
ttft = 0 | ||
error_response_code = -1 | ||
generated_text = "" | ||
error_msg = "" | ||
output_throughput = 0 | ||
total_request_time = 0 | ||
|
||
metrics = {} | ||
|
||
metrics[common_metrics.ERROR_CODE] = None | ||
metrics[common_metrics.ERROR_MSG] = "" | ||
|
||
start_time = time.monotonic() | ||
most_recent_received_token_time = time.monotonic() | ||
address = os.environ.get("AZUREAI_API_BASE") | ||
if not address: | ||
raise ValueError("the environment variable OPENAI_API_BASE must be set.") | ||
key = os.environ.get("AZUREAI_API_KEY") | ||
if not key: | ||
raise ValueError("the environment variable OPENAI_API_KEY must be set.") | ||
headers = {"Authorization": f"Bearer {key}"} | ||
if not address: | ||
raise ValueError("No host provided.") | ||
if not address.endswith("/"): | ||
address = address + "/" | ||
address += "chat/completions" | ||
try: | ||
with requests.post( | ||
address, | ||
json=body, | ||
stream=True, | ||
timeout=180, | ||
headers=headers, | ||
) as response: | ||
if response.status_code != 200: | ||
error_msg = response.text | ||
error_response_code = response.status_code | ||
response.raise_for_status() | ||
for chunk in response.iter_lines(chunk_size=None): | ||
chunk = chunk.strip() | ||
|
||
if not chunk: | ||
continue | ||
stem = "data: " | ||
chunk = chunk[len(stem) :] | ||
if chunk == b"[DONE]": | ||
continue | ||
tokens_received += 1 | ||
data = json.loads(chunk) | ||
|
||
if "error" in data: | ||
error_msg = data["error"]["message"] | ||
error_response_code = data["error"]["code"] | ||
raise RuntimeError(data["error"]["message"]) | ||
|
||
delta = data["choices"][0]["delta"] | ||
if delta.get("content", None): | ||
if not ttft: | ||
ttft = time.monotonic() - start_time | ||
time_to_next_token.append(ttft) | ||
else: | ||
time_to_next_token.append( | ||
time.monotonic() - most_recent_received_token_time | ||
) | ||
most_recent_received_token_time = time.monotonic() | ||
generated_text += delta["content"] | ||
|
||
total_request_time = time.monotonic() - start_time | ||
output_throughput = tokens_received / total_request_time | ||
|
||
except Exception as e: | ||
metrics[common_metrics.ERROR_MSG] = error_msg | ||
metrics[common_metrics.ERROR_CODE] = error_response_code | ||
print(f"Warning Or Error: {e}") | ||
print(error_response_code) | ||
|
||
metrics[common_metrics.INTER_TOKEN_LAT] = sum(time_to_next_token) #This should be same as metrics[common_metrics.E2E_LAT]. Leave it here for now | ||
metrics[common_metrics.TTFT] = ttft | ||
metrics[common_metrics.E2E_LAT] = total_request_time | ||
metrics[common_metrics.REQ_OUTPUT_THROUGHPUT] = output_throughput | ||
metrics[common_metrics.NUM_TOTAL_TOKENS] = tokens_received + prompt_len | ||
metrics[common_metrics.NUM_OUTPUT_TOKENS] = tokens_received | ||
metrics[common_metrics.NUM_INPUT_TOKENS] = prompt_len | ||
|
||
return metrics, generated_text, request_config |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an example Azure AI endpoint instead of an anyscale endpoint?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the API_BASE to .ai.azure.com