-
Notifications
You must be signed in to change notification settings - Fork 1
Reranker support #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Reranker support #23
Changes from 2 commits
d7459f7
ea2185c
ff96c91
aaa8b15
3b25d99
5544460
4fd09e4
04191fc
4af7dc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # RerankerResult | ||
|
|
||
| The `RerankerResult` class is designed to handle the response from HTTP requests and provides functions to evaluate the success of the rerank request. | ||
|
|
||
| ## Functions | ||
|
|
||
| ### sigmoid | ||
|
|
||
| Takes any real number as input and "squashes" it into a range between `0` and `1`. | ||
|
|
||
| | Argument | Type | Description | | ||
| |----------|------|-------------| | ||
| | $x | Real | The value to squash. | | ||
|
|
||
| **Returns**: Real between `0` and `1`. | ||
|
|
||
| ```4d | ||
| ASSERT(0.5=cs._Reranker.new().sigmoid(0)) | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| property rerank : cs:C1710.RerankerAPI | ||
|
|
||
| Class extends OpenAI | ||
|
|
||
| Class constructor( ... : Variant) | ||
|
|
||
| var $parameters : Collection:=Copy parameters:C1790 | ||
|
|
||
| //can't use call or apply here | ||
| Case of | ||
| : ($parameters.length>1) | ||
| Super:C1705($parameters[0]; $parameters[1]) | ||
| : ($parameters.length>0) | ||
| Super:C1705($parameters[0]) | ||
| Else | ||
| Super:C1705() | ||
| End case | ||
|
|
||
| var $properyToRemove : Text | ||
| For each ($properyToRemove; ["embeddings"; "chat"; "images"; "files"; "moderations"]) | ||
| OB REMOVE:C1226(This:C1470; $properyToRemove) | ||
| End for each | ||
|
|
||
| This:C1470.rerank:=cs:C1710.RerankerAPI.new(This:C1470) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| Class extends OpenAIAPIResource | ||
|
|
||
| Class constructor($client : cs:C1710.Reranker) | ||
| Super:C1705($client) | ||
|
|
||
|
|
||
| Function create($query : cs:C1710.RerankerQuery; $parameters : cs:C1710.RerankerParameters) : cs:C1710.RerankerResult | ||
| If (Not:C34(OB Instance of:C1731($parameters; cs:C1710.RerankerParameters))) | ||
| $parameters:=cs:C1710.OpenAIEmbeddingsParameters.new($parameters) | ||
|
||
| End if | ||
|
|
||
| var $body:=$parameters.body() | ||
| $body.query:=$query.query | ||
| $body.documents:=$query.documents | ||
|
|
||
| return This:C1470._client._post("/rerank"; $body; $parameters; cs:C1710.RerankerResult) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| property model : Text | ||
| property top_n : Integer:=3 | ||
|
|
||
| Class extends OpenAIParameters | ||
|
|
||
| Class constructor($object : Object) | ||
| Super:C1705($object) | ||
|
|
||
| Function body() : Object | ||
| var $body:=Super:C1706.body() | ||
|
|
||
| If (Length:C16(This:C1470.model)>0) | ||
| $body.model:=This:C1470.model | ||
| End if | ||
| If (This:C1470.top_n>0) | ||
| $body.top_n:=This:C1470.top_n | ||
| End if | ||
|
|
||
| return $body |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| property query : Text:="" | ||
| property documents : Collection:=[] | ||
|
|
||
| Class constructor($query : Object) | ||
|
|
||
| If ($query=Null:C1517) | ||
| return | ||
| End if | ||
|
|
||
| If (Value type:C1509($query.query)=Is text:K8:3) | ||
| This:C1470.query:=$query.query | ||
| End if | ||
|
|
||
| If (Value type:C1509($query.documents)=Is collection:K8:32) | ||
| This:C1470.documents:=$query.documents | ||
| End if |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| Class extends OpenAIResult | ||
|
|
||
| Class constructor | ||
|
|
||
| Super:C1705() | ||
|
|
||
| Function sigmoid($x : Real) : Real | ||
|
||
|
|
||
| return 1/(1+Exp:C21(-$x)) | ||
|
|
||
| Function get results : Collection | ||
| var $body:=This:C1470._objectBody() | ||
| var $results : Collection | ||
|
|
||
| Case of | ||
| : (Value type:C1509($body.results)=Is collection:K8:32) | ||
| $results:=$body.results | ||
| : (Value type:C1509($body.data)=Is collection:K8:32) | ||
| $results:=$body.data | ||
| Else | ||
| $results:=[] | ||
| End case | ||
|
|
||
| var $result : Object | ||
| var $relevance_score : Real | ||
| var $shouldNormalize : Boolean | ||
|
|
||
| For each ($result; $results) | ||
| $relevance_score:=$result.relevance_score | ||
| Case of | ||
|
||
| : ($relevance_score>1) | ||
| $shouldNormalize:=True:C214 | ||
| break | ||
| : ($relevance_score<0) | ||
| $shouldNormalize:=True:C214 | ||
| break | ||
| End case | ||
| End for each | ||
|
|
||
| If ($shouldNormalize) | ||
| For each ($result; $results) | ||
| $result.relevance_score:=This:C1470.sigmoid($result.relevance_score) | ||
| End for each | ||
| End if | ||
|
|
||
| return $results | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no _Reranker
if private, no need to doc
Just principe of returned collection result and solution