Skip to content

Reranker support#23

Open
miyako wants to merge 9 commits into4d:mainfrom
miyako:RerankerResult
Open

Reranker support#23
miyako wants to merge 9 commits into4d:mainfrom
miyako:RerankerResult

Conversation

@miyako
Copy link
Copy Markdown
Member

@miyako miyako commented Feb 7, 2026


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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RerankerParameters

Copy link
Copy Markdown
Member Author

@miyako miyako Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

**Returns**: Real between `0` and `1`.

```4d
ASSERT(0.5=cs._Reranker.new().sigmoid(0))
Copy link
Copy Markdown
Contributor

@e-marchand e-marchand Feb 7, 2026

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


Super:C1705()

Function sigmoid($x : Real) : Real
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_sigmoid private?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


For each ($result; $results)
$relevance_score:=$result.relevance_score
Case of
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe simple if

An edge case: If a provider returns all scores between 0 and 1 but they are actually logits (unlikely but possible with low-confidence results), normalization would be skipped but maybe acceptable

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

@e-marchand e-marchand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must Fix

  • RerankerAPI.4dm:9 — Change OpenAIEmbeddingsParameters to RerankerParameters
  • RerankerResult.md:18 — Fix cs._Reranker reference to correct class name

Should Fix

  • Add documentation for Reranker, RerankerAPI, RerankerQuery, RerankerParameters
  • Complete RerankerResult.md — document the results computed property and normalization behavior
  • sigmoid private

Important

The following text has been generated by AI after I suggest to study architecture, and look at alternative like delegation

Architecture — Consider delegation over inheritance

The current approach (Reranker extends OpenAI + strip properties) is fragile and conceptually misleading — /rerank is not an OpenAI endpoint, yet the class inherits OPENAI_API_KEY defaults and https://api.openai.com/v1 fallback.

Suggested alternative: Flatten to a single Reranker class using delegation. The Reranker owns its own configuration but can optionally receive an OpenAI client for shared HTTP infrastructure:

// Simple — standalone reranker with its own config
var $reranker:=cs.AIKit.Reranker.new({baseURL: "http://127.0.0.1:8080/v1"})

// With shared client — reuses auth, httpAgent, retry, timeout from an existing OpenAI instance
var $openai:=cs.AIKit.OpenAI.new({apiKey: "key"; baseURL: "https://api.cohere.com/v2"})
var $reranker:=cs.AIKit.Reranker.new($openai)

// Or both — client for infra, object for overrides
var $reranker:=cs.AIKit.Reranker.new($openai; {baseURL: "http://other-server/v1"})

The Reranker class would:

  • Store an optional _client: cs.OpenAI reference for delegation (HTTP calls, auth headers, retry logic)
  • Accept a plain Object as constructor param, auto-create an internal cs.OpenAI client from it if no client passed
  • Expose only .create($query; $parameters) directly — no .rerank.create() nesting needed since there's only one operation
  • Keep RerankerQuery, RerankerParameters, RerankerResult as-is (they're well-designed)

This removes:

  • The 5 OB REMOVE calls
  • The "OpenAI subclass for non-OpenAI endpoint" confusion
  • The stale-removal-list maintenance burden
  • The leaked .models property

@miyako
Copy link
Copy Markdown
Member Author

miyako commented Feb 9, 2026

4fd09e4

@miyako
Copy link
Copy Markdown
Member Author

miyako commented Feb 9, 2026

About delegation. Perhaps if OpenAI itself inherited an abstract base class, the non-OpenAI classes could inherit that also, but since OpenAI is currently the base class I was hesitant to make that architectural shift.

@e-marchand
Copy link
Copy Markdown
Contributor

Interesting point! I still lean toward delegation since Reranker could use other methods beyond API (OpenAI like ones), like in the Python library

I have shared with Product Team a "document" about what a reranker is and why we need it

@miyako
Copy link
Copy Markdown
Member Author

miyako commented Mar 2, 2026

I've changed the constructor to accept delegation. I did not however apply the AI suggestion to remove the "API" class, since it seems to be the uniform style used throughout AI Kit, even if the API had only a single function.

Here is the code snippet to try the new design

//%attributes = {}
/*

delegation over inheritance

*/

var $apiKey; $baseURL : Text
$apiKey:=Folder:C1567(fk desktop folder:K87:19).file("Cohere.token").getText()
$baseURL:="http://127.0.0.1:8080/v1"
var $Cohere : cs:C1710.OpenAI
$Cohere:=cs:C1710.OpenAI.new({baseURL: "https://api.cohere.ai/v2"; apiKey: $apiKey})

var $Reranker : cs:C1710.Reranker
//syntax 0 (no params)
$Reranker:=cs:C1710.Reranker.new()  //provider=openai (no rerank endpoint)

//syntax 1.a (text)
$Reranker:=cs:C1710.Reranker.new($apiKey)  //provider=openai (no rerank endpoint)
//syntax 1.b (config)
$Reranker:=cs:C1710.Reranker.new({baseURL: $baseURL})
//syntax 1.c (client)
$Reranker:=cs:C1710.Reranker.new($Cohere)

//syntax 2.a (text;text)
$Reranker:=cs:C1710.Reranker.new(""/*$apiKey*/; $baseURL)
//syntax 2.b (text;config)
$Reranker:=cs:C1710.Reranker.new($apiKey; {baseURL: "https://api.cohere.ai/v2"})
//syntax 2.c (client;config)
$Reranker:=cs.Reranker.new($Cohere; {baseURL: $baseURL; apiKey: ""})

var $query:=cs:C1710.RerankerQuery.new({query: "What is deep learning?"; documents: [\
"Deep learning is a subset of machine learning based on artificial neural networks."; \
"Apples are red and sweet fruits that grow on trees."; \
"The theory of relativity was developed by Albert Einstein."; \
"Neural networks simulate the human brain to solve complex problems."\
]})

/*
Not implemented; Cohere specific request properties:

v1
rank_fields
return_documents
max_chunks_per_doc

v2
max_tokens_per_doc
priority

*/

var $parameters:=cs:C1710.RerankerParameters.new({model: "rerank-v4.0-fast"; top_n: 3})

var $result:=$Reranker.rerank.create($query; $parameters)

SET TEXT TO PASTEBOARD:C523(JSON Stringify:C1217($result.results; *))

@e-marchand e-marchand mentioned this pull request Mar 5, 2026
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants