diff --git a/README.md b/README.md index 84bd081..f873299 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ You can install the package via pip: pip install vapi_python ``` -On Mac, you might need to install `brew install portaudio` to satisfy `pyaudio`'s dependency requirement. +On Mac, you might need to install `brew install portaudio` to satisfy `pyaudio`'s dependency requirement. ## Usage @@ -31,7 +31,9 @@ You can start a new call by calling the `start` method and passing an `assistant ```python vapi.start(assistant_id='your-assistant-id') ``` + or + ```python assistant = { 'firstMessage': 'Hey, how are you?', @@ -45,7 +47,21 @@ assistant = { vapi.start(assistant=assistant) ``` -The `start` method will initiate a new call. +The `start` method will initiate a new call. + +You can override existing assistant parameters or set variables with the `assistant_overrides` parameter. +Assume the first message is `Hey, {{name}} how are you?` and you want to set the value of `name` to `John`: + +```python +assistant_overrides = { + "recordingEnabled": False, + "variableValues": { + "name": "John" + } +} + +vapi.start(assistant_id='your-assistant-id', assistant_overrides=assistant_overrides) +``` You can stop the session by calling the `stop` method: @@ -80,5 +96,3 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` - - diff --git a/README.rst b/README.rst index f27df0f..fe0672c 100644 --- a/README.rst +++ b/README.rst @@ -75,7 +75,21 @@ or vapi.start(assistant=assistant) -The `start` method will initiate a new call. +The `start` method will initiate a new call. + +You can override existing assistant parameters or set variables with the `assistant_overrides` parameter. +Assume the first message is `Hey, {{name}} how are you?` and you want to set the value of `name` to `John`: + +.. code-block:: python + + assistant_overrides = { + "recordingEnabled": False, + "variableValues": { + "name": "John" + } + } + + vapi.start(assistant_id='your-assistant-id', assistant_overrides=assistant_overrides) You can stop the session by calling the `stop` method: diff --git a/setup.py b/setup.py index ed5e1e5..1ed5194 100644 --- a/setup.py +++ b/setup.py @@ -49,6 +49,6 @@ def read_requirements(file): test_suite='tests', tests_require=test_requirements, url='https://github.com/jordan.cde/vapi_python', - version='0.1.8', + version='0.1.9', zip_safe=False, ) diff --git a/vapi_python/daily_call.py b/vapi_python/daily_call.py index 5dcc1cd..f4a7911 100644 --- a/vapi_python/daily_call.py +++ b/vapi_python/daily_call.py @@ -1,6 +1,7 @@ import daily import threading import pyaudio +import json SAMPLE_RATE = 16000 NUM_CHANNELS = 1 @@ -160,3 +161,15 @@ def receive_bot_audio(self): if len(buffer) > 0: self.__output_audio_stream.write(buffer, CHUNK_SIZE) + + def send_app_message(self, message): + """ + Send an application message to the assistant. + + :param message: The message to send (expects a dictionary). + """ + try: + serialized_message = json.dumps(message) + self.__call_client.send_app_message(serialized_message) + except Exception as e: + print(f"Failed to send app message: {e}") diff --git a/vapi_python/vapi_python.py b/vapi_python/vapi_python.py index 44fe220..69e9797 100644 --- a/vapi_python/vapi_python.py +++ b/vapi_python/vapi_python.py @@ -6,13 +6,13 @@ CHANNELS = 1 -def create_web_call(api_url, api_key, assistant): +def create_web_call(api_url, api_key, payload): url = f"{api_url}/call/web" headers = { 'Authorization': 'Bearer ' + api_key, 'Content-Type': 'application/json' } - response = requests.post(url, headers=headers, json=assistant) + response = requests.post(url, headers=headers, json=payload) data = response.json() if response.status_code == 201: call_id = data.get('id') @@ -27,15 +27,29 @@ def __init__(self, *, api_key, api_url="https://api.vapi.ai"): self.api_key = api_key self.api_url = api_url - def start(self, *, assistant_id=None, assistant=None): + def start( + self, + *, + assistant_id=None, + assistant=None, + assistant_overrides=None, + squad_id=None, + squad=None, + ): # Start a new call if assistant_id: - assistant = {'assistantId': assistant_id} + payload = {'assistantId': assistant_id, 'assistantOverrides': assistant_overrides} elif assistant: - assistant = {'assistant': assistant} + payload = {'assistant': assistant, 'assistantOverrides': assistant_overrides} + elif squad_id: + payload = {'squadId': squad_id} + elif squad: + payload = {'squad': squad} + else: + raise Exception("Error: No assistant specified.") call_id, web_call_url = create_web_call( - self.api_url, self.api_key, assistant) + self.api_url, self.api_key, payload) if not web_call_url: raise Exception("Error: Unable to create call.") @@ -48,3 +62,34 @@ def start(self, *, assistant_id=None, assistant=None): def stop(self): self.__client.leave() self.__client = None + + def send(self, message): + """ + Send a generic message to the assistant. + + :param message: A dictionary containing the message type and content. + """ + if not self.__client: + raise Exception("Call not started. Please start the call first.") + + # Check message format here instead of serialization + if not isinstance(message, dict) or 'type' not in message: + raise ValueError("Invalid message format.") + + try: + self.__client.send_app_message(message) # Send dictionary directly + except Exception as e: + print(f"Failed to send message: {e}") + + def add_message(self, role, content): + """ + method to send text messages with specific parameters. + """ + message = { + 'type': 'add-message', + 'message': { + 'role': role, + 'content': content + } + } + self.send(message)