whatsapp-chatgpt-python 1.0.0__tar.gz → 1.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/PKG-INFO +61 -2
- whatsapp_chatgpt_python-1.0.0/whatsapp_chatgpt_python.egg-info/PKG-INFO → whatsapp_chatgpt_python-1.1.0/README.md +55 -28
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/setup.py +6 -1
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/__init__.py +2 -1
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/bot.py +55 -2
- whatsapp_chatgpt_python-1.1.0/whatsapp_chatgpt_python/config.py +53 -0
- whatsapp_chatgpt_python-1.1.0/whatsapp_chatgpt_python/voice.py +187 -0
- whatsapp_chatgpt_python-1.0.0/README.md → whatsapp_chatgpt_python-1.1.0/whatsapp_chatgpt_python.egg-info/PKG-INFO +87 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python.egg-info/SOURCES.txt +1 -0
- whatsapp_chatgpt_python-1.1.0/whatsapp_chatgpt_python.egg-info/requires.txt +8 -0
- whatsapp_chatgpt_python-1.0.0/whatsapp_chatgpt_python/config.py +0 -33
- whatsapp_chatgpt_python-1.0.0/whatsapp_chatgpt_python.egg-info/requires.txt +0 -3
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/LICENSE +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/setup.cfg +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/__init__.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/audio.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/base.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/contact.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/document.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/image.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/location.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/poll.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/registry.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/text.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/handlers/video.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/middleware/__init__.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/middleware/base.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/middleware/manager.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/models.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/notification.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/utils/__init__.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/utils/conversation.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/utils/media.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/utils/router.py +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python.egg-info/dependency_links.txt +0 -0
- {whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: whatsapp-chatgpt-python
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.1.0
|
|
4
4
|
Summary: A modern WhatsApp bot library with OpenAI GPT integration on Python
|
|
5
5
|
Home-page: https://github.com/green-api/whatsapp-chatgpt-python
|
|
6
6
|
Author: GREEN-API
|
|
@@ -15,6 +15,8 @@ License-File: LICENSE
|
|
|
15
15
|
Requires-Dist: whatsapp-chatbot-python>=0.9.6
|
|
16
16
|
Requires-Dist: openai>=1.66.3
|
|
17
17
|
Requires-Dist: requests>=2.32.3
|
|
18
|
+
Provides-Extra: voice
|
|
19
|
+
Requires-Dist: whatsapp-chatbot-python[voip]>=1.3.0; python_version >= "3.11" and extra == "voice"
|
|
18
20
|
Dynamic: author
|
|
19
21
|
Dynamic: author-email
|
|
20
22
|
Dynamic: classifier
|
|
@@ -22,6 +24,8 @@ Dynamic: description
|
|
|
22
24
|
Dynamic: description-content-type
|
|
23
25
|
Dynamic: home-page
|
|
24
26
|
Dynamic: license
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
Dynamic: provides-extra
|
|
25
29
|
Dynamic: requires-dist
|
|
26
30
|
Dynamic: requires-python
|
|
27
31
|
Dynamic: summary
|
|
@@ -146,6 +150,61 @@ bot = WhatsappGptBot(
|
|
|
146
150
|
bot.run_forever()
|
|
147
151
|
```
|
|
148
152
|
|
|
153
|
+
## Voice Calls
|
|
154
|
+
|
|
155
|
+
The bot can answer and make WhatsApp voice calls and talk with the person through an OpenAI Realtime model. The
|
|
156
|
+
caller's speech goes to the model, the model's speech goes into the call, and the caller can interrupt the model.
|
|
157
|
+
Calls need Python 3.11 or newer, a GREEN-API plan with calls and the optional dependencies:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
pip install 'whatsapp-chatgpt-python[voice]'
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
import re
|
|
165
|
+
from whatsapp_chatgpt_python import WhatsappGptBot, VoiceCallsConfig
|
|
166
|
+
|
|
167
|
+
bot = WhatsappGptBot(
|
|
168
|
+
id_instance="your-instance-id",
|
|
169
|
+
api_token_instance="your-api-token",
|
|
170
|
+
openai_api_key="your-openai-api-key",
|
|
171
|
+
system_message="You are a helpful assistant of a coffee shop.",
|
|
172
|
+
voice_calls=VoiceCallsConfig(
|
|
173
|
+
voice="marin",
|
|
174
|
+
greeting="Greet the caller and ask how you can help.",
|
|
175
|
+
),
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@bot.router.message(regexp=(r"call me", re.IGNORECASE))
|
|
180
|
+
def call_me(notification):
|
|
181
|
+
bot.voice_call(notification.chat)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
bot.run_forever()
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
- With `voice_calls` set, the bot answers incoming calls with the model. Set `answer_incoming=False` to only make
|
|
188
|
+
calls. If incoming call notifications are off and no `settings` are passed, the bot turns them on at start.
|
|
189
|
+
- `bot.voice_call(chat_id)` calls a chat and starts the conversation when the person answers. Outgoing calls work only
|
|
190
|
+
to chats that wrote or called in the last 72 hours. `bot.in_voice_call` tells whether a call is running.
|
|
191
|
+
- One call runs at a time, on its own thread, so the bot keeps answering messages during a call.
|
|
192
|
+
- The call ends when the person hangs up or after `max_call_seconds`.
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
@dataclass
|
|
196
|
+
class VoiceCallsConfig:
|
|
197
|
+
model: str = "gpt-realtime-2.1"
|
|
198
|
+
voice: str = "marin" # marin, cedar, alloy, ash, ballad, coral, echo, sage, shimmer, verse
|
|
199
|
+
instructions: Optional[str] = None # system_message by default
|
|
200
|
+
greeting: str = "Greet the caller briefly."
|
|
201
|
+
answer_incoming: bool = True
|
|
202
|
+
max_call_seconds: float = 600
|
|
203
|
+
ring_seconds: float = 60
|
|
204
|
+
vad: VoiceActivityConfig = VoiceActivityConfig() # threshold 0.5, prefix_padding_ms 400,
|
|
205
|
+
# silence_duration_ms 800, noise_reduction "near_field", "far_field" or None
|
|
206
|
+
```
|
|
207
|
+
|
|
149
208
|
## Message Handling
|
|
150
209
|
|
|
151
210
|
The bot automatically handles different types of WhatsApp messages and converts them into a format understood by
|
|
@@ -1,31 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.2
|
|
2
|
-
Name: whatsapp-chatgpt-python
|
|
3
|
-
Version: 1.0.0
|
|
4
|
-
Summary: A modern WhatsApp bot library with OpenAI GPT integration on Python
|
|
5
|
-
Home-page: https://github.com/green-api/whatsapp-chatgpt-python
|
|
6
|
-
Author: GREEN-API
|
|
7
|
-
Author-email: support@green-api.com
|
|
8
|
-
License: MIT
|
|
9
|
-
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
-
Classifier: Operating System :: OS Independent
|
|
12
|
-
Requires-Python: >=3.7
|
|
13
|
-
Description-Content-Type: text/markdown
|
|
14
|
-
License-File: LICENSE
|
|
15
|
-
Requires-Dist: whatsapp-chatbot-python>=0.9.6
|
|
16
|
-
Requires-Dist: openai>=1.66.3
|
|
17
|
-
Requires-Dist: requests>=2.32.3
|
|
18
|
-
Dynamic: author
|
|
19
|
-
Dynamic: author-email
|
|
20
|
-
Dynamic: classifier
|
|
21
|
-
Dynamic: description
|
|
22
|
-
Dynamic: description-content-type
|
|
23
|
-
Dynamic: home-page
|
|
24
|
-
Dynamic: license
|
|
25
|
-
Dynamic: requires-dist
|
|
26
|
-
Dynamic: requires-python
|
|
27
|
-
Dynamic: summary
|
|
28
|
-
|
|
29
1
|
# WhatsApp GPT Bot Library for Python
|
|
30
2
|
|
|
31
3
|
- [Документация на русском языке](./README.ru.md)
|
|
@@ -146,6 +118,61 @@ bot = WhatsappGptBot(
|
|
|
146
118
|
bot.run_forever()
|
|
147
119
|
```
|
|
148
120
|
|
|
121
|
+
## Voice Calls
|
|
122
|
+
|
|
123
|
+
The bot can answer and make WhatsApp voice calls and talk with the person through an OpenAI Realtime model. The
|
|
124
|
+
caller's speech goes to the model, the model's speech goes into the call, and the caller can interrupt the model.
|
|
125
|
+
Calls need Python 3.11 or newer, a GREEN-API plan with calls and the optional dependencies:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
pip install 'whatsapp-chatgpt-python[voice]'
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
import re
|
|
133
|
+
from whatsapp_chatgpt_python import WhatsappGptBot, VoiceCallsConfig
|
|
134
|
+
|
|
135
|
+
bot = WhatsappGptBot(
|
|
136
|
+
id_instance="your-instance-id",
|
|
137
|
+
api_token_instance="your-api-token",
|
|
138
|
+
openai_api_key="your-openai-api-key",
|
|
139
|
+
system_message="You are a helpful assistant of a coffee shop.",
|
|
140
|
+
voice_calls=VoiceCallsConfig(
|
|
141
|
+
voice="marin",
|
|
142
|
+
greeting="Greet the caller and ask how you can help.",
|
|
143
|
+
),
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@bot.router.message(regexp=(r"call me", re.IGNORECASE))
|
|
148
|
+
def call_me(notification):
|
|
149
|
+
bot.voice_call(notification.chat)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
bot.run_forever()
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
- With `voice_calls` set, the bot answers incoming calls with the model. Set `answer_incoming=False` to only make
|
|
156
|
+
calls. If incoming call notifications are off and no `settings` are passed, the bot turns them on at start.
|
|
157
|
+
- `bot.voice_call(chat_id)` calls a chat and starts the conversation when the person answers. Outgoing calls work only
|
|
158
|
+
to chats that wrote or called in the last 72 hours. `bot.in_voice_call` tells whether a call is running.
|
|
159
|
+
- One call runs at a time, on its own thread, so the bot keeps answering messages during a call.
|
|
160
|
+
- The call ends when the person hangs up or after `max_call_seconds`.
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
@dataclass
|
|
164
|
+
class VoiceCallsConfig:
|
|
165
|
+
model: str = "gpt-realtime-2.1"
|
|
166
|
+
voice: str = "marin" # marin, cedar, alloy, ash, ballad, coral, echo, sage, shimmer, verse
|
|
167
|
+
instructions: Optional[str] = None # system_message by default
|
|
168
|
+
greeting: str = "Greet the caller briefly."
|
|
169
|
+
answer_incoming: bool = True
|
|
170
|
+
max_call_seconds: float = 600
|
|
171
|
+
ring_seconds: float = 60
|
|
172
|
+
vad: VoiceActivityConfig = VoiceActivityConfig() # threshold 0.5, prefix_padding_ms 400,
|
|
173
|
+
# silence_duration_ms 800, noise_reduction "near_field", "far_field" or None
|
|
174
|
+
```
|
|
175
|
+
|
|
149
176
|
## Message Handling
|
|
150
177
|
|
|
151
178
|
The bot automatically handles different types of WhatsApp messages and converts them into a format understood by
|
|
@@ -9,7 +9,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
|
|
9
9
|
|
|
10
10
|
setup(
|
|
11
11
|
name="whatsapp-chatgpt-python",
|
|
12
|
-
version="1.
|
|
12
|
+
version="1.1.0",
|
|
13
13
|
author="GREEN-API",
|
|
14
14
|
author_email="support@green-api.com",
|
|
15
15
|
description="A modern WhatsApp bot library with OpenAI GPT integration on Python",
|
|
@@ -29,4 +29,9 @@ setup(
|
|
|
29
29
|
"openai>=1.66.3",
|
|
30
30
|
"requests>=2.32.3"
|
|
31
31
|
],
|
|
32
|
+
extras_require={
|
|
33
|
+
"voice": [
|
|
34
|
+
"whatsapp-chatbot-python[voip]>=1.3.0; python_version >= '3.11'"
|
|
35
|
+
]
|
|
36
|
+
},
|
|
32
37
|
)
|
{whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/bot.py
RENAMED
|
@@ -3,15 +3,22 @@ Main WhatsApp GPT Bot class
|
|
|
3
3
|
"""
|
|
4
4
|
import logging
|
|
5
5
|
import time
|
|
6
|
-
from typing import Dict, Type, Callable
|
|
6
|
+
from typing import Dict, Type, Callable, Optional
|
|
7
7
|
import openai
|
|
8
8
|
from whatsapp_chatbot_python import GreenAPIBot, Notification
|
|
9
9
|
|
|
10
|
-
from .config import
|
|
10
|
+
from .config import (
|
|
11
|
+
DEFAULT_MODEL,
|
|
12
|
+
ALL_MODELS,
|
|
13
|
+
INCOMING_CALL_END_STATUSES,
|
|
14
|
+
INCOMING_CALL_OFFER,
|
|
15
|
+
is_image_capable_model,
|
|
16
|
+
)
|
|
11
17
|
from .models import GPTSessionData
|
|
12
18
|
from .middleware import MiddlewareManager
|
|
13
19
|
from .handlers import MessageHandler, MessageHandlerRegistry
|
|
14
20
|
from .utils.router import setup_router_integration
|
|
21
|
+
from .voice import VoiceCalls, VoiceCallsConfig
|
|
15
22
|
|
|
16
23
|
logger = logging.getLogger('whatsapp_chatgpt_python')
|
|
17
24
|
|
|
@@ -30,6 +37,7 @@ class WhatsappGptBot(GreenAPIBot):
|
|
|
30
37
|
temperature: float = 0.5,
|
|
31
38
|
error_message: str = "Sorry, I encountered an error processing your message. Please try again.",
|
|
32
39
|
session_timeout: int = 1800, # 30 minutes
|
|
40
|
+
voice_calls: Optional[VoiceCallsConfig] = None,
|
|
33
41
|
**kwargs
|
|
34
42
|
):
|
|
35
43
|
"""
|
|
@@ -45,6 +53,7 @@ class WhatsappGptBot(GreenAPIBot):
|
|
|
45
53
|
temperature: Temperature for response generation
|
|
46
54
|
error_message: Message to send when an error occurs
|
|
47
55
|
session_timeout: Timeout for inactive sessions in seconds
|
|
56
|
+
voice_calls: Voice calls with an OpenAI Realtime model. Without it the bot does not answer calls
|
|
48
57
|
**kwargs: Additional arguments to pass to GreenAPIBot
|
|
49
58
|
"""
|
|
50
59
|
super().__init__(id_instance, api_token_instance, **kwargs)
|
|
@@ -66,8 +75,52 @@ class WhatsappGptBot(GreenAPIBot):
|
|
|
66
75
|
|
|
67
76
|
setup_router_integration(self)
|
|
68
77
|
|
|
78
|
+
self._voice: Optional[VoiceCalls] = None
|
|
79
|
+
if voice_calls is not None:
|
|
80
|
+
self._voice = VoiceCalls(
|
|
81
|
+
self.api.host, id_instance, api_token_instance, openai_api_key, voice_calls, system_message
|
|
82
|
+
)
|
|
83
|
+
if voice_calls.answer_incoming:
|
|
84
|
+
if not kwargs.get("settings"):
|
|
85
|
+
self._enable_incoming_call_webhook()
|
|
86
|
+
self.router.incoming_call()(self._answer_with_voice)
|
|
87
|
+
|
|
69
88
|
logger.info(f"WhatsApp GPT Bot initialized with model {model}")
|
|
70
89
|
|
|
90
|
+
def voice_call(self, chat_id: str) -> None:
|
|
91
|
+
"""
|
|
92
|
+
Call a chat and talk with the person through the OpenAI Realtime model when they answer.
|
|
93
|
+
The call runs in the background. Outgoing calls work only to chats that wrote or called in the last 72 hours.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
chat_id: Chat to call
|
|
97
|
+
"""
|
|
98
|
+
if self._voice is None:
|
|
99
|
+
raise RuntimeError("Voice calls are not configured, pass voice_calls to the bot")
|
|
100
|
+
self._voice.call(chat_id)
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def in_voice_call(self) -> bool:
|
|
104
|
+
"""Whether a voice conversation with the model is running now"""
|
|
105
|
+
return self._voice is not None and self._voice.busy
|
|
106
|
+
|
|
107
|
+
def _answer_with_voice(self, notification) -> None:
|
|
108
|
+
call = notification.call
|
|
109
|
+
if call.status in INCOMING_CALL_END_STATUSES and call.call_id:
|
|
110
|
+
self._voice.caller_left(call.call_id)
|
|
111
|
+
return
|
|
112
|
+
if call.status != INCOMING_CALL_OFFER or not call.sender or not call.call_id or self._voice.busy:
|
|
113
|
+
return
|
|
114
|
+
try:
|
|
115
|
+
self._voice.answer(call.sender, call.call_id, call.timestamp)
|
|
116
|
+
except Exception as e:
|
|
117
|
+
logger.error(f"Voice call failed: {e}")
|
|
118
|
+
|
|
119
|
+
def _enable_incoming_call_webhook(self) -> None:
|
|
120
|
+
settings = self.api.account.getSettings().data or {}
|
|
121
|
+
if settings.get("incomingCallWebhook") != "yes":
|
|
122
|
+
self.api.account.setSettings({"incomingCallWebhook": "yes"})
|
|
123
|
+
|
|
71
124
|
def process_chat_sync(self, notification: Notification) -> None:
|
|
72
125
|
"""
|
|
73
126
|
Synchronous wrapper for _process_chat to avoid issues with event loops
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Configuration and constants
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
DEFAULT_MODEL = "gpt-4o"
|
|
6
|
+
|
|
7
|
+
GPT4_MODELS = [
|
|
8
|
+
"gpt-4", "gpt-4-turbo", "gpt-4-turbo-preview",
|
|
9
|
+
"gpt-4-1106-preview", "gpt-4-0125-preview", "gpt-4-32k"
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
GPT4O_MODELS = [
|
|
13
|
+
"gpt-4o", "gpt-4o-mini", "gpt-4o-2024-05-13"
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
GPT35_MODELS = [
|
|
17
|
+
"gpt-3.5-turbo", "gpt-3.5-turbo-16k",
|
|
18
|
+
"gpt-3.5-turbo-1106", "gpt-3.5-turbo-0125"
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
O1_MODELS = [
|
|
22
|
+
"o1", "o1-mini", "o1-preview"
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
IMAGE_CAPABLE_MODELS = [
|
|
26
|
+
"gpt-4o", "gpt-4o-mini", "gpt-4-vision-preview",
|
|
27
|
+
"gpt-4-turbo", "gpt-4-turbo-preview"
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
ALL_MODELS = GPT4_MODELS + GPT4O_MODELS + GPT35_MODELS + O1_MODELS
|
|
31
|
+
|
|
32
|
+
DEFAULT_REALTIME_MODEL = "gpt-realtime-2.1"
|
|
33
|
+
DEFAULT_REALTIME_VOICE = "marin"
|
|
34
|
+
DEFAULT_VOICE_GREETING = "Greet the caller briefly."
|
|
35
|
+
DEFAULT_MAX_CALL_SECONDS = 600
|
|
36
|
+
DEFAULT_RING_SECONDS = 60
|
|
37
|
+
DEFAULT_ANSWER_SECONDS = 60
|
|
38
|
+
DEFAULT_VAD_THRESHOLD = 0.5
|
|
39
|
+
DEFAULT_VAD_PREFIX_PADDING_MS = 400
|
|
40
|
+
DEFAULT_VAD_SILENCE_DURATION_MS = 800
|
|
41
|
+
DEFAULT_VAD_NOISE_REDUCTION = "near_field"
|
|
42
|
+
VOICE_CALL_LANGUAGE = "adaptive"
|
|
43
|
+
VOICE_THREAD_NAME = "voice-calls"
|
|
44
|
+
INCOMING_CALL_OFFER = "offer"
|
|
45
|
+
INCOMING_CALL_END_STATUSES = ("declined", "missed", "hungUp", "hangUp")
|
|
46
|
+
VOICE_MISSING_DEPENDENCIES = (
|
|
47
|
+
"Voice calls need Python 3.11 or newer and the optional dependencies, install them with: "
|
|
48
|
+
"pip install 'whatsapp-chatgpt-python[voice]'"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def is_image_capable_model(model: str) -> bool:
|
|
53
|
+
return model in IMAGE_CAPABLE_MODELS
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Voice calls with an OpenAI Realtime model
|
|
3
|
+
"""
|
|
4
|
+
import asyncio
|
|
5
|
+
import importlib.util
|
|
6
|
+
import logging
|
|
7
|
+
import sys
|
|
8
|
+
import threading
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from time import monotonic, time
|
|
11
|
+
from typing import Optional
|
|
12
|
+
|
|
13
|
+
from .config import (
|
|
14
|
+
DEFAULT_ANSWER_SECONDS,
|
|
15
|
+
DEFAULT_MAX_CALL_SECONDS,
|
|
16
|
+
DEFAULT_REALTIME_MODEL,
|
|
17
|
+
DEFAULT_REALTIME_VOICE,
|
|
18
|
+
DEFAULT_RING_SECONDS,
|
|
19
|
+
DEFAULT_VAD_NOISE_REDUCTION,
|
|
20
|
+
DEFAULT_VAD_PREFIX_PADDING_MS,
|
|
21
|
+
DEFAULT_VAD_SILENCE_DURATION_MS,
|
|
22
|
+
DEFAULT_VAD_THRESHOLD,
|
|
23
|
+
DEFAULT_VOICE_GREETING,
|
|
24
|
+
VOICE_CALL_LANGUAGE,
|
|
25
|
+
VOICE_MISSING_DEPENDENCIES,
|
|
26
|
+
VOICE_THREAD_NAME,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
logger = logging.getLogger('whatsapp_chatgpt_python')
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class VoiceActivityConfig:
|
|
34
|
+
"""
|
|
35
|
+
Detection of the caller's speech
|
|
36
|
+
|
|
37
|
+
Attributes:
|
|
38
|
+
threshold: Speech threshold from 0 to 1, higher needs louder speech
|
|
39
|
+
prefix_padding_ms: Audio kept before the detected speech, in milliseconds
|
|
40
|
+
silence_duration_ms: Silence that ends the caller's turn, in milliseconds
|
|
41
|
+
noise_reduction: Noise reduction of the caller's audio: near_field, far_field or None
|
|
42
|
+
"""
|
|
43
|
+
threshold: float = DEFAULT_VAD_THRESHOLD
|
|
44
|
+
prefix_padding_ms: int = DEFAULT_VAD_PREFIX_PADDING_MS
|
|
45
|
+
silence_duration_ms: int = DEFAULT_VAD_SILENCE_DURATION_MS
|
|
46
|
+
noise_reduction: Optional[str] = DEFAULT_VAD_NOISE_REDUCTION
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass
|
|
50
|
+
class VoiceCallsConfig:
|
|
51
|
+
"""
|
|
52
|
+
Voice calls handled by an OpenAI Realtime model
|
|
53
|
+
|
|
54
|
+
Attributes:
|
|
55
|
+
model: Realtime model
|
|
56
|
+
voice: Voice of the model, for example marin, cedar, alloy, ash, coral, sage, shimmer, verse
|
|
57
|
+
instructions: Instructions for the voice conversation, the bot's system_message by default
|
|
58
|
+
greeting: What the model is asked to say first when the call connects
|
|
59
|
+
answer_incoming: Whether to answer incoming calls with the model
|
|
60
|
+
max_call_seconds: Longest conversation in seconds, the bot hangs up after it
|
|
61
|
+
ring_seconds: How long an outgoing call rings before the bot gives up
|
|
62
|
+
vad: Detection of the caller's speech
|
|
63
|
+
"""
|
|
64
|
+
model: str = DEFAULT_REALTIME_MODEL
|
|
65
|
+
voice: str = DEFAULT_REALTIME_VOICE
|
|
66
|
+
instructions: Optional[str] = None
|
|
67
|
+
greeting: str = DEFAULT_VOICE_GREETING
|
|
68
|
+
answer_incoming: bool = True
|
|
69
|
+
max_call_seconds: float = DEFAULT_MAX_CALL_SECONDS
|
|
70
|
+
ring_seconds: float = DEFAULT_RING_SECONDS
|
|
71
|
+
vad: VoiceActivityConfig = field(default_factory=VoiceActivityConfig)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class VoiceCalls:
|
|
75
|
+
"""
|
|
76
|
+
Runs one voice call at a time on its own thread, so the bot keeps handling messages during a call.
|
|
77
|
+
The call itself is run by the calls engine of whatsapp-chatbot-python.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
def __init__(
|
|
81
|
+
self,
|
|
82
|
+
api_url: str,
|
|
83
|
+
id_instance: str,
|
|
84
|
+
api_token_instance: str,
|
|
85
|
+
openai_api_key: str,
|
|
86
|
+
config: VoiceCallsConfig,
|
|
87
|
+
instructions: Optional[str],
|
|
88
|
+
):
|
|
89
|
+
if sys.version_info < (3, 11) or importlib.util.find_spec("aiortc") is None:
|
|
90
|
+
raise RuntimeError(VOICE_MISSING_DEPENDENCIES)
|
|
91
|
+
|
|
92
|
+
from whatsapp_chatbot_python.calls import (
|
|
93
|
+
CallOptions,
|
|
94
|
+
CallStateMachine,
|
|
95
|
+
RecordingOptions,
|
|
96
|
+
VadOptions,
|
|
97
|
+
WhatsAppCallService,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
self._config = config
|
|
101
|
+
self._service = WhatsAppCallService(
|
|
102
|
+
api_url=api_url,
|
|
103
|
+
id_instance=id_instance,
|
|
104
|
+
api_token_instance=api_token_instance,
|
|
105
|
+
openai_api_key=openai_api_key,
|
|
106
|
+
realtime_model=config.model,
|
|
107
|
+
realtime_voice=config.voice,
|
|
108
|
+
instructions=config.instructions or instructions or None,
|
|
109
|
+
options=CallOptions(
|
|
110
|
+
ring_seconds=config.ring_seconds,
|
|
111
|
+
talk_seconds=config.max_call_seconds,
|
|
112
|
+
recording=RecordingOptions(enabled=False),
|
|
113
|
+
vad=VadOptions(
|
|
114
|
+
threshold=config.vad.threshold,
|
|
115
|
+
prefix_padding_ms=config.vad.prefix_padding_ms,
|
|
116
|
+
silence_duration_ms=config.vad.silence_duration_ms,
|
|
117
|
+
noise_reduction=config.vad.noise_reduction,
|
|
118
|
+
),
|
|
119
|
+
),
|
|
120
|
+
)
|
|
121
|
+
self._fsm = CallStateMachine()
|
|
122
|
+
self._lock = threading.Lock()
|
|
123
|
+
self._session = None
|
|
124
|
+
self._loop = asyncio.new_event_loop()
|
|
125
|
+
threading.Thread(target=self._loop.run_forever, name=VOICE_THREAD_NAME, daemon=True).start()
|
|
126
|
+
|
|
127
|
+
@property
|
|
128
|
+
def busy(self) -> bool:
|
|
129
|
+
"""Whether a voice call is running now"""
|
|
130
|
+
return self._session is not None
|
|
131
|
+
|
|
132
|
+
def call(self, chat_id: str) -> None:
|
|
133
|
+
"""Calls a chat and talks with the person through the model when they answer"""
|
|
134
|
+
from whatsapp_chatbot_python.calls import CallSession
|
|
135
|
+
|
|
136
|
+
self._start(CallSession(chat_id, chat_id, VOICE_CALL_LANGUAGE, prompt=self._config.greeting))
|
|
137
|
+
|
|
138
|
+
def answer(self, sender: str, call_id: str, timestamp: Optional[float]) -> bool:
|
|
139
|
+
"""Answers an incoming call that is still ringing"""
|
|
140
|
+
from whatsapp_chatbot_python.calls import CallSession
|
|
141
|
+
|
|
142
|
+
remaining = DEFAULT_ANSWER_SECONDS - max(0.0, time() - (timestamp or time()))
|
|
143
|
+
if remaining <= 0:
|
|
144
|
+
return False
|
|
145
|
+
|
|
146
|
+
self._start(CallSession.incoming(
|
|
147
|
+
sender_id=sender,
|
|
148
|
+
call_id=call_id,
|
|
149
|
+
language=VOICE_CALL_LANGUAGE,
|
|
150
|
+
incoming_deadline=monotonic() + remaining,
|
|
151
|
+
prompt=self._config.greeting,
|
|
152
|
+
))
|
|
153
|
+
return True
|
|
154
|
+
|
|
155
|
+
def caller_left(self, call_id: str) -> None:
|
|
156
|
+
"""Tells the running call that the caller hung up before the call was answered"""
|
|
157
|
+
self._loop.call_soon_threadsafe(self._service.notify_incoming_ended, call_id)
|
|
158
|
+
|
|
159
|
+
def stop(self) -> None:
|
|
160
|
+
"""Ends the running call and stops taking new ones"""
|
|
161
|
+
self._loop.call_soon_threadsafe(self._service.request_stop)
|
|
162
|
+
|
|
163
|
+
def _start(self, session) -> None:
|
|
164
|
+
from whatsapp_chatbot_python.calls import CallEvent
|
|
165
|
+
|
|
166
|
+
with self._lock:
|
|
167
|
+
if self._session is not None:
|
|
168
|
+
raise RuntimeError("A voice call is already running")
|
|
169
|
+
self._session = session
|
|
170
|
+
|
|
171
|
+
self._fsm.apply(session, CallEvent.ENQUEUED)
|
|
172
|
+
self._fsm.apply(session, CallEvent.DEQUEUED)
|
|
173
|
+
asyncio.run_coroutine_threadsafe(self._run(session), self._loop)
|
|
174
|
+
|
|
175
|
+
async def _run(self, session) -> None:
|
|
176
|
+
try:
|
|
177
|
+
result = await self._service.execute(session, self._fsm.apply)
|
|
178
|
+
with result:
|
|
179
|
+
logger.info(
|
|
180
|
+
f"Voice call with {session.chat_id} ended: {session.state.value}"
|
|
181
|
+
+ (f" ({session.error_code})" if session.error_code else "")
|
|
182
|
+
)
|
|
183
|
+
except Exception as e:
|
|
184
|
+
logger.error(f"Voice call failed: {e}", exc_info=True)
|
|
185
|
+
finally:
|
|
186
|
+
with self._lock:
|
|
187
|
+
self._session = None
|
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: whatsapp-chatgpt-python
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: A modern WhatsApp bot library with OpenAI GPT integration on Python
|
|
5
|
+
Home-page: https://github.com/green-api/whatsapp-chatgpt-python
|
|
6
|
+
Author: GREEN-API
|
|
7
|
+
Author-email: support@green-api.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.7
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: whatsapp-chatbot-python>=0.9.6
|
|
16
|
+
Requires-Dist: openai>=1.66.3
|
|
17
|
+
Requires-Dist: requests>=2.32.3
|
|
18
|
+
Provides-Extra: voice
|
|
19
|
+
Requires-Dist: whatsapp-chatbot-python[voip]>=1.3.0; python_version >= "3.11" and extra == "voice"
|
|
20
|
+
Dynamic: author
|
|
21
|
+
Dynamic: author-email
|
|
22
|
+
Dynamic: classifier
|
|
23
|
+
Dynamic: description
|
|
24
|
+
Dynamic: description-content-type
|
|
25
|
+
Dynamic: home-page
|
|
26
|
+
Dynamic: license
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
Dynamic: provides-extra
|
|
29
|
+
Dynamic: requires-dist
|
|
30
|
+
Dynamic: requires-python
|
|
31
|
+
Dynamic: summary
|
|
32
|
+
|
|
1
33
|
# WhatsApp GPT Bot Library for Python
|
|
2
34
|
|
|
3
35
|
- [Документация на русском языке](./README.ru.md)
|
|
@@ -118,6 +150,61 @@ bot = WhatsappGptBot(
|
|
|
118
150
|
bot.run_forever()
|
|
119
151
|
```
|
|
120
152
|
|
|
153
|
+
## Voice Calls
|
|
154
|
+
|
|
155
|
+
The bot can answer and make WhatsApp voice calls and talk with the person through an OpenAI Realtime model. The
|
|
156
|
+
caller's speech goes to the model, the model's speech goes into the call, and the caller can interrupt the model.
|
|
157
|
+
Calls need Python 3.11 or newer, a GREEN-API plan with calls and the optional dependencies:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
pip install 'whatsapp-chatgpt-python[voice]'
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
import re
|
|
165
|
+
from whatsapp_chatgpt_python import WhatsappGptBot, VoiceCallsConfig
|
|
166
|
+
|
|
167
|
+
bot = WhatsappGptBot(
|
|
168
|
+
id_instance="your-instance-id",
|
|
169
|
+
api_token_instance="your-api-token",
|
|
170
|
+
openai_api_key="your-openai-api-key",
|
|
171
|
+
system_message="You are a helpful assistant of a coffee shop.",
|
|
172
|
+
voice_calls=VoiceCallsConfig(
|
|
173
|
+
voice="marin",
|
|
174
|
+
greeting="Greet the caller and ask how you can help.",
|
|
175
|
+
),
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@bot.router.message(regexp=(r"call me", re.IGNORECASE))
|
|
180
|
+
def call_me(notification):
|
|
181
|
+
bot.voice_call(notification.chat)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
bot.run_forever()
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
- With `voice_calls` set, the bot answers incoming calls with the model. Set `answer_incoming=False` to only make
|
|
188
|
+
calls. If incoming call notifications are off and no `settings` are passed, the bot turns them on at start.
|
|
189
|
+
- `bot.voice_call(chat_id)` calls a chat and starts the conversation when the person answers. Outgoing calls work only
|
|
190
|
+
to chats that wrote or called in the last 72 hours. `bot.in_voice_call` tells whether a call is running.
|
|
191
|
+
- One call runs at a time, on its own thread, so the bot keeps answering messages during a call.
|
|
192
|
+
- The call ends when the person hangs up or after `max_call_seconds`.
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
@dataclass
|
|
196
|
+
class VoiceCallsConfig:
|
|
197
|
+
model: str = "gpt-realtime-2.1"
|
|
198
|
+
voice: str = "marin" # marin, cedar, alloy, ash, ballad, coral, echo, sage, shimmer, verse
|
|
199
|
+
instructions: Optional[str] = None # system_message by default
|
|
200
|
+
greeting: str = "Greet the caller briefly."
|
|
201
|
+
answer_incoming: bool = True
|
|
202
|
+
max_call_seconds: float = 600
|
|
203
|
+
ring_seconds: float = 60
|
|
204
|
+
vad: VoiceActivityConfig = VoiceActivityConfig() # threshold 0.5, prefix_padding_ms 400,
|
|
205
|
+
# silence_duration_ms 800, noise_reduction "near_field", "far_field" or None
|
|
206
|
+
```
|
|
207
|
+
|
|
121
208
|
## Message Handling
|
|
122
209
|
|
|
123
210
|
The bot automatically handles different types of WhatsApp messages and converts them into a format understood by
|
|
@@ -6,6 +6,7 @@ whatsapp_chatgpt_python/bot.py
|
|
|
6
6
|
whatsapp_chatgpt_python/config.py
|
|
7
7
|
whatsapp_chatgpt_python/models.py
|
|
8
8
|
whatsapp_chatgpt_python/notification.py
|
|
9
|
+
whatsapp_chatgpt_python/voice.py
|
|
9
10
|
whatsapp_chatgpt_python.egg-info/PKG-INFO
|
|
10
11
|
whatsapp_chatgpt_python.egg-info/SOURCES.txt
|
|
11
12
|
whatsapp_chatgpt_python.egg-info/dependency_links.txt
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Configuration and constants
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
DEFAULT_MODEL = "gpt-4o"
|
|
6
|
-
|
|
7
|
-
GPT4_MODELS = [
|
|
8
|
-
"gpt-4", "gpt-4-turbo", "gpt-4-turbo-preview",
|
|
9
|
-
"gpt-4-1106-preview", "gpt-4-0125-preview", "gpt-4-32k"
|
|
10
|
-
]
|
|
11
|
-
|
|
12
|
-
GPT4O_MODELS = [
|
|
13
|
-
"gpt-4o", "gpt-4o-mini", "gpt-4o-2024-05-13"
|
|
14
|
-
]
|
|
15
|
-
|
|
16
|
-
GPT35_MODELS = [
|
|
17
|
-
"gpt-3.5-turbo", "gpt-3.5-turbo-16k",
|
|
18
|
-
"gpt-3.5-turbo-1106", "gpt-3.5-turbo-0125"
|
|
19
|
-
]
|
|
20
|
-
|
|
21
|
-
O1_MODELS = [
|
|
22
|
-
"o1", "o1-mini", "o1-preview"
|
|
23
|
-
]
|
|
24
|
-
|
|
25
|
-
IMAGE_CAPABLE_MODELS = [
|
|
26
|
-
"gpt-4o", "gpt-4o-mini", "gpt-4-vision-preview",
|
|
27
|
-
"gpt-4-turbo", "gpt-4-turbo-preview"
|
|
28
|
-
]
|
|
29
|
-
|
|
30
|
-
ALL_MODELS = GPT4_MODELS + GPT4O_MODELS + GPT35_MODELS + O1_MODELS
|
|
31
|
-
|
|
32
|
-
def is_image_capable_model(model: str) -> bool:
|
|
33
|
-
return model in IMAGE_CAPABLE_MODELS
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{whatsapp_chatgpt_python-1.0.0 → whatsapp_chatgpt_python-1.1.0}/whatsapp_chatgpt_python/models.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|