Skip to content

Commit 4b2f03b

Browse files
authored
486 support for pydantic v2 v1 compatible (vocodedev#490)
* Update dependencies and import statements * Update Google Cloud Text-to-Speech import * Update dependencies and imports * Update Google Cloud Text-to-Speech import * Update google-cloud-texttospeech version * Update dependencies in pyproject.toml and poetry.lock
1 parent 6f7e9cd commit 4b2f03b

File tree

28 files changed

+393
-250
lines changed

28 files changed

+393
-250
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- "3.10"
1818
- "3.11"
1919
poetry-version:
20-
- "1.4.2"
20+
- "1.7.1"
2121

2222
runs-on: ubuntu-latest
2323

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repos:
44
hooks:
55
- id: black
66
- repo: https://github.com/pre-commit/mirrors-mypy
7-
rev: v1.3.0
7+
rev: v1.8.0
88
hooks:
99
- id: mypy
1010
args: [--ignore-missing-imports, ./]

apps/telegram_bot/main.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import inspect
66
from collections import defaultdict
7-
from pydantic import BaseModel
7+
from pydantic.v1 import BaseModel
88
from typing import Tuple, Union, Optional, Dict, Type, List
99
from pydub import AudioSegment
1010
from telegram import Update
@@ -76,33 +76,36 @@
7676

7777
# Define a Voice model with id, name and description fields
7878
class Voice(BaseModel):
79-
id: Optional[str] = None # Optional id for the voice
80-
name: Optional[str] = None # Optional name for the voice
81-
description: Optional[str] = None # Optional description for the voice
79+
id: Optional[str] = None # Optional id for the voice
80+
name: Optional[str] = None # Optional name for the voice
81+
description: Optional[str] = None # Optional description for the voice
82+
8283

8384
# Array of tuples (synthesizer's voice id, nickname, description if text to voice)
8485
DEFAULT_VOICES: List[Voice] = [Voice(id=None, name="Coqui Default", description=None)]
8586

87+
8688
# Define a Chat model with voices, current_voice and current_conversation fields
8789
class Chat(BaseModel):
88-
voices: List[Voice] = DEFAULT_VOICES # List of available voices for the chat
89-
current_voice: Voice = DEFAULT_VOICES[0] # Current voice for the chat
90-
current_conversation: Optional[bytes] = None # Current conversation as a pickled object
90+
voices: List[Voice] = DEFAULT_VOICES # List of available voices for the chat
91+
current_voice: Voice = DEFAULT_VOICES[0] # Current voice for the chat
92+
current_conversation: Optional[
93+
bytes
94+
] = None # Current conversation as a pickled object
9195

9296

9397
class VocodeBotResponder:
9498
def __init__(
9599
self,
96100
transcriber: BaseTranscriber,
97101
system_prompt: str,
98-
synthesizer: BaseSynthesizer
102+
synthesizer: BaseSynthesizer,
99103
) -> None:
100104
self.transcriber = transcriber
101105
self.system_prompt = system_prompt
102106
self.synthesizer = synthesizer
103107
self.db: Dict[int, Chat] = defaultdict(Chat)
104108

105-
106109
def get_agent(self, chat_id: int) -> ChatGPTAgent:
107110
# Get current voice name and description from DB
108111
user = self.db[chat_id]
@@ -142,7 +145,9 @@ async def get_response(
142145
voice_description = user.current_voice.description
143146

144147
# If we have a Coqui voice prompt, use that. Otherwise, set ID as synthesizer expects.
145-
if voice_description is not None and isinstance(self.synthesizer, CoquiSynthesizer):
148+
if voice_description is not None and isinstance(
149+
self.synthesizer, CoquiSynthesizer
150+
):
146151
self.synthesizer.voice_prompt = voice_description
147152
elif voice_id is not None:
148153
setattr(self.synthesizer, voice_attr_of[type(self.synthesizer)], voice_id)
@@ -162,7 +167,9 @@ async def handle_telegram_start(
162167
start_text = """
163168
I'm a voice chatbot, send a voice message to me and I'll send one back!" Use /help to see available commands.
164169
"""
165-
await context.bot.send_message(chat_id=update.effective_chat.id, text=start_text)
170+
await context.bot.send_message(
171+
chat_id=update.effective_chat.id, text=start_text
172+
)
166173

167174
async def handle_telegram_message(
168175
self, update: Update, context: ContextTypes.DEFAULT_TYPE
@@ -302,7 +309,7 @@ async def handle_telegram_help(
302309
- Use /help to see this help message again.
303310
"""
304311
assert update.effective_chat, "Chat must be defined!"
305-
if isinstance(self.synthesizer, CoquiSynthesizer):
312+
if isinstance(self.synthesizer, CoquiSynthesizer):
306313
help_text += "\n- Use /create <voice_description> to create a new Coqui voice from a text prompt and switch to it."
307314
await context.bot.send_message(chat_id=update.effective_chat.id, text=help_text)
308315

apps/voice_rag/main.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from vocode.streaming.models.transcriber import (
1818
DeepgramTranscriberConfig,
19-
TimeEndpointingConfig
19+
TimeEndpointingConfig,
2020
)
2121

2222
from dotenv import load_dotenv
@@ -29,12 +29,15 @@
2929
logger = logging.getLogger(__name__)
3030
logger.setLevel(logging.DEBUG)
3131

32-
vector_db_config = PineconeConfig(
33-
index=os.getenv('PINECONE_INDEX_NAME')
34-
)
32+
# Ensure that the environment variable 'PINECONE_INDEX_NAME' is not None
33+
pinecone_index_name = os.getenv("PINECONE_INDEX_NAME")
34+
if pinecone_index_name is None:
35+
raise ValueError("Environment variable 'PINECONE_INDEX_NAME' is not set.")
36+
37+
vector_db_config = PineconeConfig(index=pinecone_index_name)
3538

36-
INITIAL_MESSAGE="Hello!"
37-
PROMPT_PREAMBLE='''
39+
INITIAL_MESSAGE = "Hello!"
40+
PROMPT_PREAMBLE = """
3841
I want you to act as an IT Architect.
3942
I will provide some details about the functionality of an application or other
4043
digital product, and it will be your job to come up with ways to integrate it
@@ -48,14 +51,16 @@
4851
- Next.js
4952
- Fastapi
5053
- Vocode.
51-
'''
54+
"""
5255

5356
TIME_ENDPOINTING_CONFIG = TimeEndpointingConfig()
5457
TIME_ENDPOINTING_CONFIG.time_cutoff_seconds = 2
5558

5659
AZURE_SYNTHESIZER_THUNK = lambda output_audio_config: AzureSynthesizer(
57-
AzureSynthesizerConfig.from_output_audio_config(output_audio_config, ),
58-
logger=logger
60+
AzureSynthesizerConfig.from_output_audio_config(
61+
output_audio_config,
62+
),
63+
logger=logger,
5964
)
6065

6166
DEEPGRAM_TRANSCRIBER_THUNK = lambda input_audio_config: DeepgramTranscriber(
@@ -64,7 +69,7 @@
6469
endpointing_config=TIME_ENDPOINTING_CONFIG,
6570
min_interrupt_confidence=0.9,
6671
),
67-
logger=logger
72+
logger=logger,
6873
)
6974

7075
conversation_router = ConversationRouter(
@@ -73,9 +78,8 @@
7378
initial_message=BaseMessage(text=INITIAL_MESSAGE),
7479
prompt_preamble=PROMPT_PREAMBLE,
7580
vector_db_config=vector_db_config,
76-
logger=logger,
7781
),
78-
logger=logger
82+
logger=logger,
7983
),
8084
synthesizer_thunk=AZURE_SYNTHESIZER_THUNK,
8185
transcriber_thunk=DEEPGRAM_TRANSCRIBER_THUNK,

playground/streaming/agent/chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import typing
33
from dotenv import load_dotenv
44
from playground.streaming.tracing_utils import make_parser_and_maybe_trace
5-
from pydantic import BaseModel
5+
from pydantic.v1 import BaseModel
66
from vocode.streaming.action.base_action import BaseAction
77
from vocode.streaming.action.factory import ActionFactory
88
from vocode.streaming.action.worker import ActionsWorker

0 commit comments

Comments
 (0)