15
15
using OpenAI . Chat ;
16
16
using SmartTalk . Core . Services . Agents ;
17
17
using SmartTalk . Core . Services . Http ;
18
+ using SmartTalk . Core . Services . Http . Clients ;
18
19
using SmartTalk . Messages . Constants ;
19
20
using SmartTalk . Core . Services . Jobs ;
20
21
using SmartTalk . Core . Services . PhoneOrder ;
30
31
using SmartTalk . Messages . Commands . AiSpeechAssistant ;
31
32
using SmartTalk . Messages . Commands . PhoneOrder ;
32
33
using SmartTalk . Messages . Dto . Agent ;
34
+ using SmartTalk . Messages . Enums . Agent ;
33
35
using SmartTalk . Messages . Enums . PhoneOrder ;
34
36
using JsonSerializer = System . Text . Json . JsonSerializer ;
35
37
using RecordingResource = Twilio . Rest . Api . V2010 . Account . Call . RecordingResource ;
@@ -56,8 +58,8 @@ public class AiSpeechAssistantService : IAiSpeechAssistantService
56
58
private readonly IMapper _mapper ;
57
59
private readonly OpenAiSettings _openAiSettings ;
58
60
private readonly TwilioSettings _twilioSettings ;
61
+ private readonly ISmartiesClient _smartiesClient ;
59
62
private readonly ZhiPuAiSettings _zhiPuAiSettings ;
60
- private readonly IAgentDataProvider _agentDataProvider ;
61
63
private readonly IPhoneOrderService _phoneOrderService ;
62
64
private readonly ISmartTalkHttpClientFactory _httpClientFactory ;
63
65
private readonly IPhoneOrderDataProvider _phoneOrderDataProvider ;
@@ -68,8 +70,8 @@ public AiSpeechAssistantService(
68
70
IMapper mapper ,
69
71
OpenAiSettings openAiSettings ,
70
72
TwilioSettings twilioSettings ,
73
+ ISmartiesClient smartiesClient ,
71
74
ZhiPuAiSettings zhiPuAiSettings ,
72
- IAgentDataProvider agentDataProvider ,
73
75
IPhoneOrderService phoneOrderService ,
74
76
ISmartTalkHttpClientFactory httpClientFactory ,
75
77
IPhoneOrderDataProvider phoneOrderDataProvider ,
@@ -79,9 +81,9 @@ public AiSpeechAssistantService(
79
81
_mapper = mapper ;
80
82
_openAiSettings = openAiSettings ;
81
83
_twilioSettings = twilioSettings ;
84
+ _smartiesClient = smartiesClient ;
82
85
_zhiPuAiSettings = zhiPuAiSettings ;
83
86
_phoneOrderService = phoneOrderService ;
84
- _agentDataProvider = agentDataProvider ;
85
87
_httpClientFactory = httpClientFactory ;
86
88
_backgroundJobClient = backgroundJobClient ;
87
89
_phoneOrderDataProvider = phoneOrderDataProvider ;
@@ -153,21 +155,21 @@ public async Task ReceivePhoneRecordingStatusCallbackAsync(ReceivePhoneRecording
153
155
{
154
156
Log . Information ( "Handling receive phone record: {@command}" , command ) ;
155
157
156
- var record = await _phoneOrderDataProvider . GetPhoneOrderRecordBySessionIdAsync ( command . CallSid , cancellationToken ) . ConfigureAwait ( false ) ;
157
-
158
+ var ( record , agent , aiSpeechAssistant ) = await _phoneOrderDataProvider . GetRecordWithAgentAndAssistantAsync ( command . CallSid , cancellationToken ) . ConfigureAwait ( false ) ;
159
+
158
160
Log . Information ( "Get phone order record: {@record}" , record ) ;
159
161
160
162
record . Url = command . RecordingUrl ;
161
163
record . Status = PhoneOrderRecordStatus . Sent ;
162
-
163
- var agent = await _agentDataProvider . GetAgentByIdAsync ( record . AgentId , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
164
-
164
+
165
165
ChatClient client = new ( "gpt-4o-audio-preview" , _openAiSettings . ApiKey ) ;
166
166
var audioFileRawBytes = await _httpClientFactory . GetAsync < byte [ ] > ( record . Url , cancellationToken ) . ConfigureAwait ( false ) ;
167
167
var audioData = BinaryData . FromBytes ( audioFileRawBytes ) ;
168
168
List < ChatMessage > messages =
169
169
[
170
- new SystemChatMessage ( "你是一名電話錄音的分析員,通過聽取錄音內容和語氣情緒作出精確分析,冩出一份分析報告。\n \n 分析報告的格式:交談主題:xxx\n \n 內容摘要:xxx \n \n 客人情感與情緒: xxx \n \n 待辦事件: \n 1.xxx\n 2.xxx \n \n 客人下單內容(如果沒有則忽略):1. 牛肉(1箱)\n 2.雞腿肉(1箱)" ) ,
170
+ new SystemChatMessage ( string . IsNullOrEmpty ( aiSpeechAssistant ? . CustomRecordAnalyzePrompt )
171
+ ? "你是一名電話錄音的分析員,通過聽取錄音內容和語氣情緒作出精確分析,冩出一份分析報告。\n \n 分析報告的格式:交談主題:xxx\n \n 內容摘要:xxx \n \n 客人情感與情緒: xxx \n \n 待辦事件: \n 1.xxx\n 2.xxx \n \n 客人下單內容(如果沒有則忽略):1. 牛肉(1箱)\n 2.雞腿肉(1箱)"
172
+ : aiSpeechAssistant . CustomRecordAnalyzePrompt ) ,
171
173
new UserChatMessage ( ChatMessageContentPart . CreateInputAudioPart ( audioData , ChatInputAudioFormat . Wav ) ) ,
172
174
new UserChatMessage ( "幫我根據錄音生成分析報告:" )
173
175
] ;
@@ -178,6 +180,9 @@ public async Task ReceivePhoneRecordingStatusCallbackAsync(ReceivePhoneRecording
178
180
Log . Information ( "sales record analyze report:" + completion . Content . FirstOrDefault ( ) ? . Text ) ;
179
181
record . TranscriptionText = completion . Content . FirstOrDefault ( ) ? . Text ;
180
182
183
+ if ( agent . SourceSystem == AgentSourceSystem . Smarties )
184
+ await _smartiesClient . CallBackSmartiesAiSpeechAssistantRecordAsync ( new AiSpeechAssistantCallBackRequestDto { CallSid = command . CallSid , RecordUrl = record . Url , RecordAnalyzeReport = record . TranscriptionText } , cancellationToken ) . ConfigureAwait ( false ) ;
185
+
181
186
if ( ! string . IsNullOrEmpty ( agent . WechatRobotKey ) )
182
187
await _phoneOrderService . SendWorkWeChatRobotNotifyAsync ( audioFileRawBytes , agent . WechatRobotKey , "錄音分析報告:\n " + record . TranscriptionText , cancellationToken ) . ConfigureAwait ( false ) ;
183
188
0 commit comments