-
Notifications
You must be signed in to change notification settings - Fork 28
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
refactor assistant streaming and create OpenAI compliant base class #425
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import json | ||
from typing import AsyncIterator, cast | ||
|
||
from ragna.core import PackageRequirement, RagnaException, Requirement, Source | ||
|
||
from ._api import ApiAssistant | ||
from ._http_api import HttpApiAssistant | ||
|
||
|
||
class AnthropicApiAssistant(ApiAssistant): | ||
class AnthropicAssistant(HttpApiAssistant): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Driveby rename to align it with other provider base classes. |
||
_API_KEY_ENV_VAR = "ANTHROPIC_API_KEY" | ||
_MODEL: str | ||
|
||
|
@@ -36,15 +35,12 @@ def _instructize_system_prompt(self, sources: list[Source]) -> str: | |
+ "</documents>" | ||
) | ||
|
||
async def _call_api( | ||
self, prompt: str, sources: list[Source], *, max_new_tokens: int | ||
async def answer( | ||
self, prompt: str, sources: list[Source], *, max_new_tokens: int = 256 | ||
) -> AsyncIterator[str]: | ||
import httpx_sse | ||
|
||
# See https://docs.anthropic.com/claude/reference/messages_post | ||
# See https://docs.anthropic.com/claude/reference/streaming | ||
async with httpx_sse.aconnect_sse( | ||
self._client, | ||
async for data in self._stream_sse( | ||
"POST", | ||
"https://api.anthropic.com/v1/messages", | ||
headers={ | ||
|
@@ -61,23 +57,19 @@ async def _call_api( | |
"temperature": 0.0, | ||
"stream": True, | ||
}, | ||
) as event_source: | ||
await self._assert_api_call_is_success(event_source.response) | ||
|
||
async for sse in event_source.aiter_sse(): | ||
data = json.loads(sse.data) | ||
# See https://docs.anthropic.com/claude/reference/messages-streaming#raw-http-stream-response | ||
if "error" in data: | ||
raise RagnaException(data["error"].pop("message"), **data["error"]) | ||
elif data["type"] == "message_stop": | ||
break | ||
elif data["type"] != "content_block_delta": | ||
continue | ||
): | ||
# See https://docs.anthropic.com/claude/reference/messages-streaming#raw-http-stream-response | ||
if "error" in data: | ||
raise RagnaException(data["error"].pop("message"), **data["error"]) | ||
elif data["type"] == "message_stop": | ||
break | ||
elif data["type"] != "content_block_delta": | ||
continue | ||
|
||
yield cast(str, data["delta"].pop("text")) | ||
yield cast(str, data["delta"].pop("text")) | ||
|
||
|
||
class ClaudeOpus(AnthropicApiAssistant): | ||
class ClaudeOpus(AnthropicAssistant): | ||
"""[Claude 3 Opus](https://docs.anthropic.com/claude/docs/models-overview) | ||
|
||
!!! info "Required environment variables" | ||
|
@@ -92,7 +84,7 @@ class ClaudeOpus(AnthropicApiAssistant): | |
_MODEL = "claude-3-opus-20240229" | ||
|
||
|
||
class ClaudeSonnet(AnthropicApiAssistant): | ||
class ClaudeSonnet(AnthropicAssistant): | ||
"""[Claude 3 Sonnet](https://docs.anthropic.com/claude/docs/models-overview) | ||
|
||
!!! info "Required environment variables" | ||
|
@@ -107,7 +99,7 @@ class ClaudeSonnet(AnthropicApiAssistant): | |
_MODEL = "claude-3-sonnet-20240229" | ||
|
||
|
||
class ClaudeHaiku(AnthropicApiAssistant): | ||
class ClaudeHaiku(AnthropicAssistant): | ||
"""[Claude 3 Haiku](https://docs.anthropic.com/claude/docs/models-overview) | ||
|
||
!!! info "Required environment variables" | ||
|
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
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.
The
def _call_api
abstraction fordef answer
was just a remnant of an old implementation that I forgot to clean up earlier:ragna/ragna/assistants/_api.py
Lines 32 to 38 in 84cf4f6
This PR removes it and all subclass simply implement
def answer
directly.