Commit 9e0ee1f 1 parent 4373d1e commit 9e0ee1f Copy full SHA for 9e0ee1f
File tree 1 file changed +27
-1
lines changed
1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change 1
1
use crate :: api_handler:: call_request:: call_gpt;
2
2
use crate :: helpers:: command_lines:: PrintCommand ;
3
3
use crate :: models:: general:: llm:: Message ;
4
+ use serde:: de:: DeserializeOwned ;
5
+ use serde:: Deserialize ;
4
6
5
7
6
8
// Extend AI function for specific output
@@ -35,7 +37,7 @@ pub async fn ai_task_request(
35
37
msg_context : String ,
36
38
agent_position : & str ,
37
39
agent_operation : & str ,
38
- function_pass : for <' a > fn ( & ' a str ) -> & ' static str , ) -> String
40
+ function_pass : for <' a > fn ( & ' a str ) -> & ' static str , ) -> String
39
41
{
40
42
// Расширяет функцию с помощью extend_ai_function, чтобы создать сообщение для LLM.
41
43
let extend_message: Message = extend_ai_function ( function_pass, & msg_context) ;
@@ -55,6 +57,30 @@ pub async fn ai_task_request(
55
57
}
56
58
57
59
60
+ /*
61
+ Эта функция ai_task_request_decoded предназначена для вызова GPT через асинхронную функцию ai_task_request,
62
+ получения строки-ответа, а затем преобразования (десериализации)
63
+ этой строки в объект нужного типа T с использованием библиотеки serde.
64
+ */
65
+ pub async fn ai_task_request_decoded < T : DeserializeOwned > ( // тип T должен поддерживать десериализацию из JSON.
66
+ msg_context : String ,
67
+ agent_position : & str ,
68
+ agent_operation : & str ,
69
+ function_pass : for <' a > fn ( & ' a str ) -> & ' static str ,
70
+ ) -> T {
71
+
72
+ let llm_response: String = ai_task_request (
73
+ msg_context,
74
+ agent_position,
75
+ agent_operation,
76
+ function_pass
77
+ ) . await ;
78
+ // Десериализует строку JSON в объект типа T
79
+ let decoded_response: T = serde_json:: from_str ( & llm_response. as_str ( ) ) . expect ( "Could not deserialize GPT response from serde_json" ) ;
80
+ decoded_response
81
+ }
82
+
83
+
58
84
#[ cfg( test) ]
59
85
mod tests {
60
86
use super :: * ;
You can’t perform that action at this time.
0 commit comments