@@ -46,9 +46,9 @@ impl ChatRole {
46
46
match self {
47
47
ChatRole :: User =>"user" ,
48
48
ChatRole :: Bot =>match brain{
49
- ChatbotBrain :: OpenAIGPT4
50
- | ChatbotBrain :: TekniumOpenHermes25Mistral7B
51
- | ChatbotBrain :: Starling7b => "assistant" ,
49
+ ChatbotBrain :: OpenAIGPT4 | ChatbotBrain :: TekniumOpenHermes25Mistral7B | ChatbotBrain :: Starling7b => {
50
+ "assistant"
51
+ }
52
52
ChatbotBrain :: GrypheMythoMaxL213b =>"model" ,
53
53
} ,
54
54
ChatRole :: System =>"system" ,
@@ -69,11 +69,7 @@ impl ChatbotBrain {
69
69
!matches ! ( self , Self :: OpenAIGPT4 )
70
70
}
71
71
72
- fn get_system_message (
73
- & self ,
74
- knowledge_base : & KnowledgeBase ,
75
- context : & str ,
76
- ) -> anyhow:: Result < serde_json:: Value > {
72
+ fn get_system_message ( & self , knowledge_base : & KnowledgeBase , context : & str ) -> anyhow:: Result < serde_json:: Value > {
77
73
match self {
78
74
Self :: OpenAIGPT4 =>{
79
75
let system_prompt = std:: env:: var ( "CHATBOT_CHATGPT_SYSTEM_PROMPT" ) ?;
@@ -242,10 +238,7 @@ impl Document {
242
238
. take ( 32 )
243
239
. map ( char:: from)
244
240
. collect ( ) ;
245
- let timestamp =SystemTime :: now ( )
246
- . duration_since ( UNIX_EPOCH )
247
- . unwrap ( )
248
- . as_millis ( ) ;
241
+ let timestamp =SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . unwrap ( ) . as_millis ( ) ;
249
242
Document {
250
243
id,
251
244
text : text. to_string ( ) ,
@@ -275,9 +268,7 @@ async fn get_openai_chatgpt_answer<M: Serialize>(messages: M) -> anyhow::Result<
275
268
. json :: < serde_json:: Value > ( )
276
269
. await ?;
277
270
278
- let response = response[ "choices" ]
279
- . as_array ( )
280
- . context ( "No data returned from OpenAI" ) ?[ 0 ] [ "message" ] [ "content" ]
271
+ let response = response[ "choices" ] . as_array ( ) . context ( "No data returned from OpenAI" ) ?[ 0 ] [ "message" ] [ "content" ]
281
272
. as_str ( )
282
273
. context ( "The reponse content from OpenAI was not a string" ) ?
283
274
. to_string ( ) ;
@@ -449,12 +440,11 @@ async fn do_chatbot_get_history(user: &User, limit: usize) -> anyhow::Result<Vec
449
440
. as_str ( )
450
441
. context ( "Error parsing text" ) ?
451
442
. to_string ( ) ;
452
- let model: ChatbotBrain = serde_json :: from_value ( m [ "document" ] [ "model" ] . to_owned ( ) )
453
- . context ( "Error parsing model" ) ?;
443
+ let model: ChatbotBrain =
444
+ serde_json :: from_value ( m [ "document" ] [ "model" ] . to_owned ( ) ) . context ( "Error parsing model" ) ?;
454
445
let model: & str = model. into ( ) ;
455
- let knowledge_base: KnowledgeBase =
456
- serde_json:: from_value ( m[ "document" ] [ "knowledge_base" ] . to_owned ( ) )
457
- . context ( "Error parsing knowledge_base" ) ?;
446
+ let knowledge_base: KnowledgeBase = serde_json:: from_value ( m[ "document" ] [ "knowledge_base" ] . to_owned ( ) )
447
+ . context ( "Error parsing knowledge_base" ) ?;
458
448
let knowledge_base: & str = knowledge_base. into ( ) ;
459
449
Ok ( HistoryMessage {
460
450
side,
@@ -538,17 +528,24 @@ async fn process_message(
538
528
Some ( std:: env:: var ( "CHATBOT_DATABASE_URL" ) . expect ( "CHATBOT_DATABASE_URL not set" ) ) ,
539
529
) ;
540
530
let context = collection
541
- . query ( )
542
- . vector_recall ( & data. question , & pipeline, Some ( json ! ( {
543
- "instruction" : "Represent the Wikipedia question for retrieving supporting documents: "
544
- } ) . into ( ) ) )
545
- . limit ( 5 )
546
- . fetch_all ( )
547
- . await ?
548
- . into_iter ( )
549
- . map ( |( _, context, metadata) |format ! ( "\n \n #### Document {}:\n {}\n \n " , metadata[ "id" ] , context) )
550
- . collect :: < Vec < String > > ( )
551
- . join ( "\n " ) ;
531
+ . query ( )
532
+ . vector_recall (
533
+ & data. question ,
534
+ & pipeline,
535
+ Some (
536
+ json ! ( {
537
+ "instruction" : "Represent the Wikipedia question for retrieving supporting documents: "
538
+ } )
539
+ . into ( ) ,
540
+ ) ,
541
+ )
542
+ . limit ( 5 )
543
+ . fetch_all ( )
544
+ . await ?
545
+ . into_iter ( )
546
+ . map ( |( _, context, metadata) |format ! ( "\n \n #### Document {}:\n {}\n \n " , metadata[ "id" ] , context) )
547
+ . collect :: < Vec < String > > ( )
548
+ . join ( "\n " ) ;
552
549
553
550
let history_collection =Collection :: new (
554
551
"ChatHistory" ,
@@ -590,49 +587,47 @@ async fn process_message(
590
587
. await ?;
591
588
messages. reverse ( ) ;
592
589
593
- let ( mut history, _) =
594
- messages
595
- . into_iter ( )
596
- . fold ( ( Vec :: new ( ) , None ) , |( mut new_history, role) , value|{
597
- let current_role: ChatRole =
598
- serde_json:: from_value ( value[ "document" ] [ "role" ] . to_owned ( ) )
599
- . expect ( "Error parsing chat role" ) ;
600
- if let Some ( role) = role{
601
- if role == current_role{
602
- match role{
603
- ChatRole :: User => new_history. push (
604
- serde_json:: json!( {
605
- "role" : ChatRole :: Bot . to_model_specific_role( & brain) ,
606
- "content" : "*no response due to error*"
607
- } )
608
- . into ( ) ,
609
- ) ,
610
- ChatRole :: Bot => new_history. push (
611
- serde_json:: json!( {
612
- "role" : ChatRole :: User . to_model_specific_role( & brain) ,
613
- "content" : "*no response due to error*"
614
- } )
615
- . into ( ) ,
616
- ) ,
617
- _ =>panic ! ( "Too many system messages" ) ,
618
- }
590
+ let ( mut history, _) = messages
591
+ . into_iter ( )
592
+ . fold ( ( Vec :: new ( ) , None ) , |( mut new_history, role) , value|{
593
+ let current_role: ChatRole =
594
+ serde_json:: from_value ( value[ "document" ] [ "role" ] . to_owned ( ) ) . expect ( "Error parsing chat role" ) ;
595
+ if let Some ( role) = role{
596
+ if role == current_role{
597
+ match role{
598
+ ChatRole :: User => new_history. push (
599
+ serde_json:: json!( {
600
+ "role" : ChatRole :: Bot . to_model_specific_role( & brain) ,
601
+ "content" : "*no response due to error*"
602
+ } )
603
+ . into ( ) ,
604
+ ) ,
605
+ ChatRole :: Bot => new_history. push (
606
+ serde_json:: json!( {
607
+ "role" : ChatRole :: User . to_model_specific_role( & brain) ,
608
+ "content" : "*no response due to error*"
609
+ } )
610
+ . into ( ) ,
611
+ ) ,
612
+ _ =>panic ! ( "Too many system messages" ) ,
619
613
}
620
- let new_message: pgml:: types:: Json = serde_json:: json!( {
621
- "role" : current_role. to_model_specific_role( & brain) ,
622
- "content" : value[ "document" ] [ "text" ]
623
- } )
624
- . into ( ) ;
625
- new_history. push ( new_message) ;
626
- } else if matches ! ( current_role, ChatRole :: User ) {
627
- let new_message: pgml:: types:: Json = serde_json:: json!( {
628
- "role" : current_role. to_model_specific_role( & brain) ,
629
- "content" : value[ "document" ] [ "text" ]
630
- } )
631
- . into ( ) ;
632
- new_history. push ( new_message) ;
633
614
}
634
- ( new_history, Some ( current_role) )
635
- } ) ;
615
+ let new_message: pgml:: types:: Json = serde_json:: json!( {
616
+ "role" : current_role. to_model_specific_role( & brain) ,
617
+ "content" : value[ "document" ] [ "text" ]
618
+ } )
619
+ . into ( ) ;
620
+ new_history. push ( new_message) ;
621
+ } else if matches ! ( current_role, ChatRole :: User ) {
622
+ let new_message: pgml:: types:: Json = serde_json:: json!( {
623
+ "role" : current_role. to_model_specific_role( & brain) ,
624
+ "content" : value[ "document" ] [ "text" ]
625
+ } )
626
+ . into ( ) ;
627
+ new_history. push ( new_message) ;
628
+ }
629
+ ( new_history, Some ( current_role) )
630
+ } ) ;
636
631
637
632
let system_message = brain. get_system_message ( & knowledge_base, & context) ?;
638
633
history. insert ( 0 , system_message. into ( ) ) ;
@@ -657,8 +652,7 @@ async fn process_message(
657
652
. into ( ) ,
658
653
) ;
659
654
660
- let update_history =
661
- UpdateHistory :: new ( history_collection, user_document, brain, knowledge_base) ;
655
+ let update_history =UpdateHistory :: new ( history_collection, user_document, brain, knowledge_base) ;
662
656
663
657
if brain. is_open_source ( ) {
664
658
let op =OpenSourceAI :: new ( Some (