Skip to content

Add TogetherAI chat provider - #14957

Merged
JinHai-CN merged 5 commits into
infiniflow:mainfrom
tmimmanuel:go/togetherai-chat
May 19, 2026
Merged

JinHai-CN merged 5 commits into
infiniflow:mainfrom
tmimmanuel:go/togetherai-chat

Conversation

@tmimmanuel

Copy link
Copy Markdown
Contributor

What

  • Add TogetherAI as a chat provider backed by its OpenAI-compatible /v1/chat/completions API
  • Register TogetherAI in the Go model factory and provider config
  • Support non-streaming chat, SSE streaming chat, model listing, and connection checks

Notes

  • Uses the current TogetherAI OpenAI-compatible base URL https://api.together.ai/v1
  • Forwards documented chat parameters from ChatConfig: max_tokens, temperature, top_p, stop, and GPT-OSS reasoning_effort
  • Routes Together reasoning traces from reasoning / reasoning_content into ReasonContent

Tests

  • go test -vet=off -run TestTogetherAI -count=1 ./internal/entity/models
  • go test -vet=off -count=1 ./internal/entity/models

Refs #14736

@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label May 15, 2026
@coderabbitai

coderabbitai Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds TogetherAI provider support: configuration, factory routing, a TogetherAI ModelDriver with synchronous and SSE streaming chat, model listing/check, interface stubs, and tests validating chat, streaming, listing, connection, and error paths.

Changes

TogetherAI Provider Integration

Layer / File(s) Summary
Configuration and factory routing
conf/models/togetherai.json, internal/entity/models/factory.go
TogetherAI provider metadata (base URL, chat/models suffixes) and three chat model entries are added; factory routing includes "togetherai" → NewTogetherAIModel.
Driver type, constructor, and sync chat
internal/entity/models/togetherai.go
Defines TogetherAIModel, HTTP client/transport tuning, region-aware base URL resolution, chat payload builders (including conditional reasoning_effort), and ChatWithMessages with validation, authenticated POST, response parsing, upstream error handling, and reasoning fallback.
SSE streaming chat
internal/entity/models/togetherai.go
Implements ChatStreamlyWithSender to POST SSE (Accept: text/event-stream), parse data: events, emit reasoning and content deltas to the sender, and handle terminal [DONE]/finish_reason.
Model listing, connection, and stubs
internal/entity/models/togetherai.go
Adds ListModels and CheckConnection (region-aware GET and model ID filtering) and stubbed unsupported methods returning a standardized “no such method” error.
Integration-style tests
internal/entity/models/togetherai_test.go
httptest helper and tests covering Name(), factory routing, non-streamed and streamed chat flows (including SSE chunk handling), missing-model error, ListModels/CheckConnection, and unsupported-method errors.

Sequence Diagram

sequenceDiagram
  participant Client
  participant TogetherAIModel
  participant TogetherAI_API
  Client->>TogetherAIModel: ChatWithMessages / ChatStreamlyWithSender (model, messages, API key)
  TogetherAIModel->>TogetherAI_API: POST /chat/completions (Bearer token, JSON or SSE)
  TogetherAI_API-->>TogetherAIModel: 200 JSON response (choices, reasoning) or SSE data: events
  TogetherAIModel-->>Client: aggregated message content + reasoning (and final [DONE] for streams)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

💞 feature, size:XL

Suggested reviewers

  • Haruko386

Poem

🐰 I hopped into code with a curious grin,
Wired TogetherAI so new chats could begin.
Streams and syncs, models lined in a row,
Tests watch the packets and waving they go.
A rabbit cheers softly: ready to show!

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description is missing the required 'Type of change' section from the template. While it provides context and implementation details, it doesn't follow the template structure. Add the 'Type of change' section to the description and select 'New Feature' to align with the template requirements.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding TogetherAI as a new chat provider, which is the primary objective of this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/entity/models/togetherai.go`:
- Around line 236-239: The request is created with context.Background() in the
http.NewRequestWithContext call which provides no overall deadline for
streaming; change the code to accept a context.Context parameter from the caller
(preferred) or create a context.WithTimeout (e.g., 10m) and use that context
when calling http.NewRequestWithContext so streaming requests can be cancelled;
ensure you cancel the timeout context via its cancel func to avoid leaks and
update any caller sites (or add clear docs) if you opt to require a ctx
parameter; reference the http.NewRequestWithContext invocation and the
transport.ResponseHeaderTimeout setting when making the change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5f3df89d-8754-4814-b179-88c7e36316ac

📥 Commits

Reviewing files that changed from the base of the PR and between 09d4504 and 805e701.

📒 Files selected for processing (4)
  • conf/models/togetherai.json
  • internal/entity/models/factory.go
  • internal/entity/models/togetherai.go
  • internal/entity/models/togetherai_test.go

Comment thread internal/entity/models/togetherai.go Resolved

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
internal/entity/models/togetherai.go (1)

284-298: 💤 Low value

Inconsistent reasoning handling between streaming and non-streaming.

In ChatWithMessages (lines 193-196), Reasoning is used as a fallback only when ReasoningContent is empty. Here in streaming, both fields are sent separately if present in the same delta. While SSE deltas typically populate only one field per chunk, this creates a semantic inconsistency.

Consider aligning the behavior—either treat Reasoning as a fallback in streaming as well, or document that streaming may emit both fields independently.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/entity/models/togetherai.go` around lines 284 - 298, The streaming
handler currently sends both choice.Delta.ReasoningContent and
choice.Delta.Reasoning when present, causing semantic mismatch with
ChatWithMessages which treats Reasoning as a fallback; change the logic in the
streaming delta handling (the block that calls sender with
choice.Delta.ReasoningContent, choice.Delta.Reasoning, and choice.Delta.Content)
so that you send ReasoningContent when non-empty and only send Reasoning if
ReasoningContent is empty (i.e., use an if / else if between ReasoningContent
and Reasoning), leaving the Content branch unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@internal/entity/models/togetherai.go`:
- Around line 284-298: The streaming handler currently sends both
choice.Delta.ReasoningContent and choice.Delta.Reasoning when present, causing
semantic mismatch with ChatWithMessages which treats Reasoning as a fallback;
change the logic in the streaming delta handling (the block that calls sender
with choice.Delta.ReasoningContent, choice.Delta.Reasoning, and
choice.Delta.Content) so that you send ReasoningContent when non-empty and only
send Reasoning if ReasoningContent is empty (i.e., use an if / else if between
ReasoningContent and Reasoning), leaving the Content branch unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5b324a06-8512-4b4b-a025-b103e13c7ba2

📥 Commits

Reviewing files that changed from the base of the PR and between 805e701 and cce0fde.

📒 Files selected for processing (1)
  • internal/entity/models/togetherai.go

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @tmimmanuel, thank you for your contribution.
I haven’t been able to find any official documentation for some of these sections, so please let me know where I can find the relevant documentation.

Comment thread internal/entity/models/togetherai.go Resolved
Comment thread internal/entity/models/togetherai.go Resolved
@tmimmanuel
tmimmanuel requested a review from Haruko386 May 18, 2026 13:58
@JinHai-CN

Copy link
Copy Markdown
Member

@tmimmanuel, would you please fix the conflicts?

@tmimmanuel
tmimmanuel force-pushed the go/togetherai-chat branch from aec19bd to df6cd50 Compare May 19, 2026 03:14
@tmimmanuel

Copy link
Copy Markdown
Contributor Author

@JinHai-CN
I fixed the conflict and CI is clean.

Haruko386

This comment was marked as outdated.

@Haruko386 Haruko386 added the ci Continue Integration label May 19, 2026
Comment thread internal/entity/models/togetherai.go Resolved
@codecov

codecov Bot commented May 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.16%. Comparing base (4c9529e) to head (3fb53fb).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14957   +/-   ##
=======================================
  Coverage   94.16%   94.16%           
=======================================
  Files          10       10           
  Lines         703      703           
  Branches      112      112           
=======================================
  Hits          662      662           
  Misses         25       25           
  Partials       16       16           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@tmimmanuel Let me know if I was wrong

}
if chatModelConfig.Stop != nil {
reqBody["stop"] = *chatModelConfig.Stop
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you forget #14957 (comment)
or plz let me know if was wrong

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am sorry, I forgot by mistake. I will fix now.

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants