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

Update Anthropic assistants #380

Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 0 additions & 1 deletion docs/examples/gallery_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#
# - [Anthropic](https://www.anthropic.com/)
# - [ragna.assistants.Claude][]
# - [ragna.assistants.ClaudeInstant][]
# - [Cohere](https://cohere.com/)
# - [ragna.assistants.Command][]
# - [ragna.assistants.CommandLight][]
Expand Down
1 change: 0 additions & 1 deletion docs/tutorials/gallery_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
#
# - [Anthropic](https://www.anthropic.com/)
# - [ragna.assistants.Claude][]
# - [ragna.assistants.ClaudeInstant][]
# - [Cohere](https://cohere.com/)
# - [ragna.assistants.Command][]
# - [ragna.assistants.CommandLight][]
Expand Down
3 changes: 1 addition & 2 deletions ragna/assistants/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
__all__ = [
"Claude",
"ClaudeInstant",
"Command",
"CommandLight",
"GeminiPro",
Expand All @@ -14,7 +13,7 @@
]

from ._ai21labs import Jurassic2Ultra
from ._anthropic import Claude, ClaudeInstant
from ._anthropic import Claude
from ._cohere import Command, CommandLight
from ._demo import RagnaDemoAssistant
from ._google import GeminiPro, GeminiUltra
Expand Down
53 changes: 23 additions & 30 deletions ragna/assistants/_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@ def _extra_requirements(cls) -> list[Requirement]:
def display_name(cls) -> str:
return f"Anthropic/{cls._MODEL}"

def _instructize_prompt(self, prompt: str, sources: list[Source]) -> str:
# See https://docs.anthropic.com/claude/docs/introduction-to-prompt-design#human--assistant-formatting
def _instructize_system_prompt(self, sources: list[Source]) -> str:
# See https://docs.anthropic.com/claude/docs/system-prompts
instruction = (
"\n\nHuman: "
"Use the following pieces of context to answer the question at the end. "
"If you don't know the answer, just say so. Don't try to make up an answer.\n"
"Use the following documents to answer the prompt. "
"If you don't know the answer, just say so. Don't try to make up an answer."
"Only use the included documents below to generate the answer.\n"
)
instruction += "\n\n".join(source.content for source in sources)
return f"{instruction}\n\nQuestion: {prompt}\n\nAssistant:"
return instruction

async def _call_api(
self, prompt: str, sources: list[Source], *, max_new_tokens: int
) -> 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,
"POST",
"https://api.anthropic.com/v1/complete",
"https://api.anthropic.com/v1/messages",
headers={
"accept": "application/json",
"anthropic-version": "2023-06-01",
Expand All @@ -46,8 +47,14 @@ async def _call_api(
},
json={
"model": self._MODEL,
"prompt": self._instructize_prompt(prompt, sources),
"max_tokens_to_sample": max_new_tokens,
"system": self._instructize_system_prompt(sources),
"messages": [
{
"role": "user",
"content": prompt,
}
],
"max_tokens": max_new_tokens,
"temperature": 0.0,
"stream": True,
},
Expand All @@ -56,29 +63,15 @@ async def _call_api(

async for sse in event_source.aiter_sse():
data = json.loads(sse.data)
if data["type"] != "completion":
continue
elif "error" in 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["stop_reason"] is not None:
elif data["type"] == "message_stop":
break
elif data["type"] != "content_block_delta":
continue

yield cast(str, data["completion"])


class ClaudeInstant(AnthropicApiAssistant):
"""[Claude Instant](https://docs.anthropic.com/claude/reference/selecting-a-model)

!!! info "Required environment variables"

- `ANTHROPIC_API_KEY`

!!! info "Required packages"

- `httpx_sse`
"""

_MODEL = "claude-instant-1"
yield cast(str, data["delta"].pop("text"))


class Claude(AnthropicApiAssistant):
Expand All @@ -93,4 +86,4 @@ class Claude(AnthropicApiAssistant):
- `httpx_sse`
"""

_MODEL = "claude-2"
_MODEL = "claude-3-opus-20240229"