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

completion() for fireworks #329

Merged
merged 1 commit into from
Oct 25, 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
44 changes: 41 additions & 3 deletions llama_stack/providers/adapters/inference/fireworks/fireworks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
get_sampling_options,
process_chat_completion_response,
process_chat_completion_stream_response,
process_completion_response,
process_completion_stream_response,
)
from llama_stack.providers.utils.inference.prompt_adapter import (
chat_completion_request_to_prompt,
completion_request_to_prompt,
)

from .config import FireworksImplConfig
Expand Down Expand Up @@ -60,7 +63,35 @@ async def completion(
stream: Optional[bool] = False,
logprobs: Optional[LogProbConfig] = None,
) -> AsyncGenerator:
raise NotImplementedError()
request = CompletionRequest(
model=model,
content=content,
sampling_params=sampling_params,
response_format=response_format,
stream=stream,
logprobs=logprobs,
)
client = Fireworks(api_key=self.config.api_key)
if stream:
return self._stream_completion(request, client)
else:
return await self._nonstream_completion(request, client)

async def _nonstream_completion(
self, request: CompletionRequest, client: Fireworks
) -> CompletionResponse:
params = self._get_params(request)
r = await client.completion.acreate(**params)
return process_completion_response(r, self.formatter)

async def _stream_completion(
self, request: CompletionRequest, client: Fireworks
) -> AsyncGenerator:
params = self._get_params(request)

stream = client.completion.acreate(**params)
async for chunk in process_completion_stream_response(stream, self.formatter):
yield chunk

async def chat_completion(
self,
Expand Down Expand Up @@ -110,8 +141,15 @@ async def _stream_chat_completion(
):
yield chunk

def _get_params(self, request: ChatCompletionRequest) -> dict:
prompt = chat_completion_request_to_prompt(request, self.formatter)
def _get_params(self, request) -> dict:
prompt = ""
if type(request) == ChatCompletionRequest:
prompt = chat_completion_request_to_prompt(request, self.formatter)
elif type(request) == CompletionRequest:
prompt = completion_request_to_prompt(request, self.formatter)
else:
raise ValueError(f"Unknown request type {type(request)}")

# Fireworks always prepends with BOS
if prompt.startswith("<|begin_of_text|>"):
prompt = prompt[len("<|begin_of_text|>") :]
Expand Down
4 changes: 3 additions & 1 deletion llama_stack/providers/tests/inference/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ async def test_completion(inference_settings):
"remote::ollama",
"remote::tgi",
"remote::together",
"remote::fireworks",
):
pytest.skip("Other inference providers don't support completion() yet")

Expand Down Expand Up @@ -167,7 +168,7 @@ async def test_completion(inference_settings):
]

assert all(isinstance(chunk, CompletionResponseStreamChunk) for chunk in chunks)
assert len(chunks) == 51
assert len(chunks) >= 1
Copy link
Contributor Author

@dineshyv dineshyv Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashwinb, this is needed since the number of chunks each provider returns is not consistent and does not depend on the max tokens.

last = chunks[-1]
assert last.stop_reason == StopReason.out_of_tokens

Expand All @@ -182,6 +183,7 @@ async def test_completions_structured_output(inference_settings):
"meta-reference",
"remote::tgi",
"remote::together",
"remote::fireworks",
):
pytest.skip(
"Other inference providers don't support structured output in completions yet"
Expand Down