api key configs

This commit is contained in:
Ajay Raj 2023-03-20 20:19:33 -07:00
commit dfb249e13d
5 changed files with 29 additions and 15 deletions

View file

@ -2,7 +2,6 @@ import logging
from dotenv import load_dotenv
import os
from vocode.helpers import create_microphone_input_and_speaker_output
import vocode
from vocode.turn_based.agent.chat_gpt_agent import ChatGPTAgent
from vocode.turn_based.synthesizer.azure_synthesizer import AzureSynthesizer
from vocode.turn_based.transcriber.whisper_transcriber import WhisperTranscriber
@ -22,12 +21,17 @@ if __name__ == "__main__":
conversation = TurnBasedConversation(
input_device=microphone_input,
output_device=speaker_output,
transcriber=WhisperTranscriber(),
transcriber=WhisperTranscriber(api_key=os.getenv("OPENAI_API_KEY")),
agent=ChatGPTAgent(
system_prompt="The AI is having a pleasant conversation about life",
initial_message="Hello!",
api_key=os.getenv("OPENAI_API_KEY"),
),
synthesizer=AzureSynthesizer(
sampling_rate=speaker_output.sampling_rate,
api_key=os.getenv("AZURE_SPEECH_KEY"),
region=os.getenv("AZURE_SPEECH_REGION"),
),
synthesizer=AzureSynthesizer(sampling_rate=speaker_output.sampling_rate),
logger=logger,
)
print("Starting conversation. Press Ctrl+C to exit.")

View file

@ -4,4 +4,4 @@ from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("VOCODE_API_KEY")
base_url = os.getenv("VOCODE_BASE_URL", "api.vocode.dev")
base_url = os.getenv("VOCODE_BASE_URL", "api.vocode.dev")

View file

@ -1,4 +1,6 @@
import os
from typing import Optional
import openai
from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
@ -16,12 +18,16 @@ class ChatGPTAgent(BaseAgent):
def __init__(
self,
system_prompt: str,
api_key: Optional[str] = None,
initial_message: Optional[str] = None,
model_name: str = "gpt-3.5-turbo",
temperature: float = 0.7,
max_tokens: int = 100,
):
super().__init__(initial_message=initial_message)
openai.api_key = os.getenv("OPENAI_API_KET", api_key)
if not openai.api_key:
raise ValueError("OpenAI API key not provided")
self.prompt = ChatPromptTemplate.from_messages(
[
SystemMessagePromptTemplate.from_template(system_prompt),

View file

@ -1,19 +1,22 @@
import os
from dotenv import load_dotenv
from typing import Optional
import azure.cognitiveservices.speech as speechsdk
from pydub import AudioSegment
from vocode.turn_based.synthesizer.base_synthesizer import BaseSynthesizer
load_dotenv()
class AzureSynthesizer(BaseSynthesizer):
def __init__(self, sampling_rate: int):
def __init__(
self,
sampling_rate: int,
api_key: Optional[str] = None,
region: Optional[str] = None,
):
self.sampling_rate = sampling_rate
speech_config = speechsdk.SpeechConfig(
subscription=os.environ.get("AZURE_SPEECH_KEY"),
region=os.environ.get("AZURE_SPEECH_REGION"),
subscription=os.getenv("AZURE_SPEECH_KEY", api_key),
region=os.getenv("AZURE_SPEECH_REGION", region),
)
if self.sampling_rate == 44100:
speech_config.set_speech_synthesis_output_format(

View file

@ -1,17 +1,18 @@
from typing import Optional
from pydub import AudioSegment
import io
import os
from dotenv import load_dotenv
import openai
from vocode.turn_based.transcriber.base_transcriber import BaseTranscriber
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
class WhisperTranscriber(BaseTranscriber):
def __init__(self, api_key: Optional[str] = None):
openai.api_key = os.getenv("OPENAI_API_KEY", api_key)
if not openai.api_key:
raise ValueError("OpenAI API key not provided")
def transcribe(self, audio_segment: AudioSegment) -> str:
in_memory_wav = io.BytesIO()
audio_segment.export(in_memory_wav, format="wav")