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

✨ Add initial support for anthropic #1

Merged
merged 2 commits into from
Sep 28, 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
55 changes: 55 additions & 0 deletions ask/anthropic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
from anthropic import Anthropic
from typing import Optional

from anthropic.types import TextBlock
from .bot_service import BotService
from .renderer import AbstractRenderer

ANTHROPIC_API_KEY_NAME = "ANTHROPIC_API_KEY"


class AnthropicService(BotService):
def __init__(self, prompt, renderer: AbstractRenderer, line_target=0) -> None:
self.renderer = renderer
if ANTHROPIC_API_KEY_NAME not in os.environ:
self.renderer.print(
f"""

Please get a Anthropic API key from https://console.anthropic.com/
and set in the environment variable {ANTHROPIC_API_KEY_NAME}

"""
)
self._available = False
return
self._available = True

self.client = Anthropic(
api_key=os.environ.get(ANTHROPIC_API_KEY_NAME),
)
self._available = True

@property
def available(self) -> bool:
return self._available

def process(self, user_input: str) -> Optional[str]:
try:
message = self.client.messages.create(
max_tokens=4906,
messages=[
{
"role": "user",
"content": user_input,
}
],
model="claude-3-5-sonnet-20240620",
)
content = message.content[0]
if type(content) is TextBlock:
return content.text
except Exception as e:
print(f"\nCannot process prompt \n{user_input}\n", e)

return None
3 changes: 3 additions & 0 deletions ask/ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .transcribe import register_transcribed_text, stop_transcribe
from .config import load_config, default_parse_args
from .gemini import Gemini
from .anthropic import AnthropicService
from .ollama import Ollama
from .renderer import RichRenderer, AbstractRenderer
from .bot_service import BotService
Expand Down Expand Up @@ -62,6 +63,8 @@ def run(
match config.service.provider.lower():
case "ollama":
Service = Ollama
case "anthropic":
Service = AnthropicService
case _:
Service = Gemini
service = Service(renderer=renderer, prompt=prompt, line_target=config.line_target)
Expand Down
2 changes: 1 addition & 1 deletion ask/bot_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(
pass

@abstractmethod
def process(self, user_input: Optional[str]) -> Optional[str]:
def process(self, user_input: str) -> Optional[str]:
pass

@property
Expand Down
2 changes: 1 addition & 1 deletion ask/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Ollama(BotService):
def available(self) -> bool:
return True

def process(self, user_input: Optional[str]) -> Optional[str]:
def process(self, user_input: str) -> Optional[str]:
response = requests.post(
"http://localhost:11434/api/generate",
json={"model": "qwen2.5:1.5b", "prompt": user_input, "stream": False},
Expand Down
4 changes: 4 additions & 0 deletions ask/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import os
from ..gemini import API_KEY_NAME
from ..anthropic import ANTHROPIC_API_KEY_NAME

# Safety check to ensure that API_KEY is not passed into unit tests since this
# could have unintended side effect from environment leaking in
if API_KEY_NAME in os.environ:
os.environ.pop(API_KEY_NAME)
if ANTHROPIC_API_KEY_NAME in os.environ:
os.environ.pop(ANTHROPIC_API_KEY_NAME)

assert API_KEY_NAME not in os.environ
assert ANTHROPIC_API_KEY_NAME not in os.environ
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
anthropic
configparser
google-generativeai
prompt_toolkit
Expand Down