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

View file

@ -1,4 +1,6 @@
import os
from typing import Optional from typing import Optional
import openai
from langchain.prompts import ( from langchain.prompts import (
ChatPromptTemplate, ChatPromptTemplate,
MessagesPlaceholder, MessagesPlaceholder,
@ -16,12 +18,16 @@ class ChatGPTAgent(BaseAgent):
def __init__( def __init__(
self, self,
system_prompt: str, system_prompt: str,
api_key: Optional[str] = None,
initial_message: Optional[str] = None, initial_message: Optional[str] = None,
model_name: str = "gpt-3.5-turbo", model_name: str = "gpt-3.5-turbo",
temperature: float = 0.7, temperature: float = 0.7,
max_tokens: int = 100, max_tokens: int = 100,
): ):
super().__init__(initial_message=initial_message) 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( self.prompt = ChatPromptTemplate.from_messages(
[ [
SystemMessagePromptTemplate.from_template(system_prompt), SystemMessagePromptTemplate.from_template(system_prompt),

View file

@ -1,19 +1,22 @@
import os import os
from dotenv import load_dotenv from typing import Optional
import azure.cognitiveservices.speech as speechsdk import azure.cognitiveservices.speech as speechsdk
from pydub import AudioSegment from pydub import AudioSegment
from vocode.turn_based.synthesizer.base_synthesizer import BaseSynthesizer from vocode.turn_based.synthesizer.base_synthesizer import BaseSynthesizer
load_dotenv()
class AzureSynthesizer(BaseSynthesizer): 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 self.sampling_rate = sampling_rate
speech_config = speechsdk.SpeechConfig( speech_config = speechsdk.SpeechConfig(
subscription=os.environ.get("AZURE_SPEECH_KEY"), subscription=os.getenv("AZURE_SPEECH_KEY", api_key),
region=os.environ.get("AZURE_SPEECH_REGION"), region=os.getenv("AZURE_SPEECH_REGION", region),
) )
if self.sampling_rate == 44100: if self.sampling_rate == 44100:
speech_config.set_speech_synthesis_output_format( speech_config.set_speech_synthesis_output_format(

View file

@ -1,17 +1,18 @@
from typing import Optional
from pydub import AudioSegment from pydub import AudioSegment
import io import io
import os import os
from dotenv import load_dotenv
import openai import openai
from vocode.turn_based.transcriber.base_transcriber import BaseTranscriber from vocode.turn_based.transcriber.base_transcriber import BaseTranscriber
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
class WhisperTranscriber(BaseTranscriber): 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: def transcribe(self, audio_segment: AudioSegment) -> str:
in_memory_wav = io.BytesIO() in_memory_wav = io.BytesIO()
audio_segment.export(in_memory_wav, format="wav") audio_segment.export(in_memory_wav, format="wav")