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

Anthropic v0.7 #437

Merged
merged 4 commits into from
Nov 30, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ credentials.json
benchmark_results/
private.key
dump.rdb
.idea
56 changes: 35 additions & 21 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ six = "^1.16.0"
opentelemetry-sdk = "^1.17.0"
janus = "^1.0.0"
scipy = "^1.10.1"
anthropic = "^0.2.9"
anthropic = "^0.7.1"

elevenlabs = {version = "^0.2.6", optional = true}
google-cloud-texttospeech = {version = "^2.14.1", optional = true}
Expand Down
36 changes: 17 additions & 19 deletions vocode/streaming/agent/anthropic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(

# streaming not well supported by langchain, so we will connect directly
self.anthropic_client = (
anthropic.Client(api_key=anthropic_api_key)
anthropic.AsyncAnthropic(api_key=anthropic_api_key)
if agent_config.generate_responses
else None
)
Expand Down Expand Up @@ -98,25 +98,23 @@ async def generate_response(
self.memory.chat_memory.messages.append(bot_memory_message)
prompt = self.llm._convert_messages_to_prompt(self.memory.chat_memory.messages)

streamed_response = await self.anthropic_client.acompletion_stream(
prompt=prompt,
max_tokens_to_sample=self.agent_config.max_tokens_to_sample,
model=self.agent_config.model_name,
)

buffer = ""
async for message in streamed_response:
completion = message["completion"]
delta = completion[len(bot_memory_message.content + buffer) :]
buffer += delta

sentence, remainder = get_sentence_from_buffer(buffer)
if self.anthropic_client:
streamed_response = await self.anthropic_client.completions.create(
prompt=prompt,
max_tokens_to_sample=self.agent_config.max_tokens_to_sample,
model=self.agent_config.model_name,
stream=True,
)

if sentence:
bot_memory_message.content = bot_memory_message.content + sentence
buffer = remainder
yield sentence, True
continue
buffer = ""
async for completion in streamed_response:
buffer += completion.completion
sentence, remainder = get_sentence_from_buffer(buffer)
if sentence:
bot_memory_message.content = bot_memory_message.content + sentence
buffer = remainder
yield sentence, True
continue

def update_last_bot_message_on_cut_off(self, message: str):
for memory_message in self.memory.chat_memory.messages[::-1]:
Expand Down
Loading