Skip to content

Add OpenAI adapter and runtime support for ReFRAG - #142

Merged
m1rl0k merged 6 commits into
testfrom
open-ai-support
Dec 31, 2025
Merged

m1rl0k merged 6 commits into
testfrom
open-ai-support

Conversation

@m1rl0k

@m1rl0k m1rl0k commented Dec 30, 2025

Copy link
Copy Markdown
Collaborator

Introduces scripts/refrag_openai.py, providing an OpenAI-based adapter for decoder-side ReFRAG operations. Updates requirements.txt to require openai>=2.11.0. Modifies context_answer.py and query_expand.py to support OpenAI as a runtime, including auto-detection via OPENAI_API_KEY and proper client instantiation. Decoder availability checks and prompt handling are updated to accommodate the new OpenAI integration.

Introduces scripts/refrag_openai.py, providing an OpenAI-based adapter for decoder-side ReFRAG operations. Updates requirements.txt to require openai>=2.11.0. Modifies context_answer.py and query_expand.py to support OpenAI as a runtime, including auto-detection via OPENAI_API_KEY and proper client instantiation. Decoder availability checks and prompt handling are updated to accommodate the new OpenAI integration.
@m1rl0k m1rl0k self-assigned this Dec 30, 2025
@m1rl0k
m1rl0k requested a review from voarsh2 December 30, 2025 23:08

@voarsh2 voarsh2 left a comment

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.

Some notes:

force_json mismatch (likely breakage) - query_expand.py + refrag_openai.py
query_expand.py asks for a JSON array, but OpenAIRefragClient sets response_format={"type":"json_object"} when force_json=True. That combination is inconsistent and can cause hard failures or malformed outputs.
Ask: either switch output to an object schema (e.g., {"queries":[...]}) or don’t use json_object and just parse an array defensively.

Auto-selecting OpenAI when OPENAI_API_KEY exists (behavioral surprise) - context_answer.py
Runtime auto-detect now prefers OpenAI if OPENAI_API_KEY is set. In many environments that key may exist incidentally, causing silent routing to OpenAI (cost, latency, privacy, different outputs).
Ask: require explicit REFRAG_RUNTIME=openai (or gate auto-detect behind an opt-in env flag), and log the selected runtime.

Async event-loop bug potential generate_pseudo_tags_batch()
It calls run_until_complete after grabbing/creating an event loop. If invoked from an already-running loop (common in async servers), this can raise at runtime.
Ask: provide an async variant and keep a safe sync wrapper (or detect running loop and avoid run_until_complete).

Autodetect of REFRAG_RUNTIME is now opt-in via REFRAG_RUNTIME_AUTODETECT to prevent unintended API calls. Query expansion prompts for OpenAI and GLM now require a JSON object with a 'queries' key. Added an async variant generate_pseudo_tags_batch_async to refrag_openai.py and improved event loop handling for batch pseudo tag generation.
Updated .env.example and CONFIGURATION.md to add OpenAI as a supported decoder backend, document new environment variables for OpenAI integration, and clarify runtime selection with an opt-in auto-detection mechanism. Expanded documentation for decoder configuration and query expansion to reflect these changes.
@m1rl0k
m1rl0k requested a review from voarsh2 December 30, 2025 23:41
Matches 1793a46 pattern: detect running loop and use ThreadPoolExecutor
to avoid RuntimeError when generate_pseudo_tags_batch is called from
async context (e.g., FastMCP server handlers).
@voarsh2

voarsh2 commented Dec 30, 2025

Copy link
Copy Markdown
Contributor

Re-checking

Eliminated the REFRAG_RUNTIME_AUTODETECT environment variable and all related auto-detection logic from configuration, documentation, and code. The decoder runtime must now be set explicitly, defaulting to 'llamacpp' if unset, to prevent unintended API calls.
@m1rl0k
m1rl0k merged commit 985f995 into test Dec 31, 2025
1 check passed
@m1rl0k
m1rl0k deleted the open-ai-support branch January 1, 2026 04:21
m1rl0k added a commit that referenced this pull request Mar 1, 2026
Add OpenAI adapter and runtime support for ReFRAG
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.

3 participants