Skip to content

Commit 1b80649

Browse files
authored
Merge pull request #1021 from WolframResearch/feature/reranking-speed-improvements
Optimized reranking prompt to reduce output token counts
2 parents 1b6a690 + 86d33cb commit 1b80649

File tree

7 files changed

+276
-35
lines changed

7 files changed

+276
-35
lines changed

Source/Chatbook/Handlers.wl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ addHandlerArguments // beginDefinition;
2727
addHandlerArguments[ args_ ] :=
2828
addHandlerArguments[ $ChatHandlerData, Association @ args ];
2929

30+
addHandlerArguments[ current_? AssociationQ, new_? AssociationQ ] /; AnyTrue[ new, AssociationQ ] := Enclose[
31+
$ChatHandlerData = ConfirmBy[ combineNestedHandlerData[ current, new ], AssociationQ, "AddHandlerArguments" ],
32+
throwInternalFailure
33+
];
34+
3035
addHandlerArguments[ current_? AssociationQ, new_? AssociationQ ] :=
3136
$ChatHandlerData = <| current, new |>;
3237

@@ -37,6 +42,19 @@ addHandlerArguments[ current_, new_? AssociationQ ] := (
3742

3843
addHandlerArguments // endDefinition;
3944

45+
(* ::**************************************************************************************************************:: *)
46+
(* ::Subsubsection::Closed:: *)
47+
(*combineNestedHandlerData*)
48+
combineNestedHandlerData // beginDefinition;
49+
combineNestedHandlerData[ as1_Association, as2_Association ] := combineNestedHandlerData0 @ { as1, as2 };
50+
combineNestedHandlerData // endDefinition;
51+
52+
combineNestedHandlerData0 // beginDefinition;
53+
combineNestedHandlerData0[ { as1_Association, as2_Association } ] := Merge[ { as1, as2 }, combineNestedHandlerData0 ];
54+
combineNestedHandlerData0[ { value_ } ] := value;
55+
combineNestedHandlerData0[ { _, value_ } ] := value;
56+
combineNestedHandlerData0 // endDefinition;
57+
4058
(* ::**************************************************************************************************************:: *)
4159
(* ::Subsection::Closed:: *)
4260
(*applyHandlerFunction*)

Source/Chatbook/LLMUtilities.wl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ llmSynthesize[ prompt: $$llmPrompt ] :=
3434
llmSynthesize[ prompt, <| |> ];
3535

3636
llmSynthesize[ prompt: $$llmPrompt, evaluator_Association ] := Enclose[
37-
ConfirmMatch[ llmSynthesize0[ prompt, evaluator, 1 ], Except[ "", _String ], "Result" ],
37+
ConfirmMatch[
38+
llmSynthesize0[ prompt, evaluator, 1 ],
39+
If[ MatchQ[ Flatten @ { evaluator[ "StopTokens" ] }, { __String } ], _String, Except[ "", _String ] ],
40+
"Result"
41+
],
3842
throwInternalFailure
3943
];
4044

@@ -70,19 +74,25 @@ llmSynthesizeSubmit[ prompt: $$llmPrompt, callback_ ] :=
7074
llmSynthesizeSubmit[ prompt, <| |>, callback ];
7175

7276
llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] := Enclose[
73-
Module[ { evaluator, prompt, messages, config, chunks, handlers, keys },
77+
Module[ { evaluator, prompt, messages, config, chunks, allowEmpty, handlers, keys, auth },
7478

75-
evaluator = ConfirmBy[
76-
<| $defaultLLMSynthesizeEvaluator, DeleteCases[ evaluator0, Automatic | _Missing ] |>,
77-
AssociationQ,
78-
"Evaluator"
79+
evaluator = Replace[
80+
ConfirmBy[
81+
<| $defaultLLMSynthesizeEvaluator, DeleteCases[ evaluator0, Automatic | _Missing ] |>,
82+
AssociationQ,
83+
"Evaluator"
84+
],
85+
Verbatim[ Verbatim ][ value_ ] :> value,
86+
{ 1 }
7987
];
8088

8189
prompt = ConfirmMatch[ truncatePrompt[ prompt0, evaluator ], $$llmPrompt, "Prompt" ];
8290
messages = { <| "Role" -> "User", "Content" -> prompt |> };
8391
config = LLMConfiguration @ evaluator;
8492
chunks = Internal`Bag[ ];
8593

94+
allowEmpty = MatchQ[ Flatten @ { evaluator[ "StopTokens" ] }, { __String } ];
95+
8696
handlers = <|
8797
"BodyChunkReceived" -> Function[
8898
Internal`StuffBag[ chunks, # ]
@@ -93,7 +103,7 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
93103
$lastSynthesizeSubmitLog = data;
94104
strings = extractBodyChunks @ data;
95105
Which[
96-
MatchQ[ strings, { __String } ],
106+
MatchQ[ strings, { __String } ] || (allowEmpty && strings === { }),
97107
With[ { s = StringJoin @ strings }, callback[ s, #1 ] ],
98108
FailureQ @ strings,
99109
callback[ strings, #1 ],
@@ -106,10 +116,12 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
106116

107117
keys = { "BodyChunk", "BodyChunkProcessed", "StatusCode", "EventName" };
108118

119+
auth = Lookup[ evaluator, "Authentication", $llmSynthesizeAuthentication ];
120+
109121
setServiceCaller @ LLMServices`ChatSubmit[
110122
messages,
111123
config,
112-
Authentication -> $llmSynthesizeAuthentication,
124+
Authentication -> auth,
113125
HandlerFunctions -> handlers,
114126
HandlerFunctionsKeys -> keys,
115127
"TestConnection" -> False

Source/Chatbook/PromptGenerators/EmbeddingContext.wl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $smallContextStringLength = 8000;
2222
(*getSmallContextString*)
2323
getSmallContextString // beginDefinition;
2424

25-
getSmallContextString // Options = { "IncludeSystemMessage" -> False };
25+
getSmallContextString // Options = { "IncludeSystemMessage" -> False, "SingleMessageTemplate" -> Automatic };
2626

2727
getSmallContextString[ messages0: { ___Association }, opts: OptionsPattern[ ] ] := Enclose[
2828
Catch @ Module[ { messages, string },

0 commit comments

Comments
 (0)