Skip to main content

langchain-together

This package contains the LangChain integration with Together AI.

Installation

pip install langchain-together

Chat Models

ChatTogether supports the various models available via the Together API:

from langchain_together import ChatTogether
import os


os.environ["TOGETHER_API_KEY"] = "my-key"


llm = ChatTogether(
    model="Qwen/Qwen2.5-7B-Instruct-Turbo",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    # api_key="...",  # if not set in environment variable
)

Structured Outputs, Function Calls, JSON Mode

ChatTogether supports structured outputs using Pydantic models, dictionaries, or JSON schemas. This feature allows you to get reliable, structured responses from Together AI models. See here the docs for more info about function calling and structured outputs

from langchain_together import ChatTogether
from pydantic import BaseModel, Field
from typing import Optional

class Joke(BaseModel):
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline of the joke")
    rating: Optional[int] = Field(default=None, description="How funny the joke is from 1-10")

# Use a model that supports function calling
llm = ChatTogether(model="Qwen/Qwen2.5-7B-Instruct-Turbo")
structured_llm = llm.with_structured_output(Joke.model_json_schema(), method="json_schema")

result = structured_llm.invoke("Tell me a joke about programming")
print(f"Setup: {result.setup}")
print(f"Punchline: {result.punchline}")
print(f"Rating: {result.rating}")

Function Calling

# Use a model that supports function calling
llm = ChatTogether(model="Qwen/Qwen2.5-7B-Instruct-Turbo")
structured_llm = llm.with_structured_output(Joke, method="function_calling")

result = structured_llm.invoke("Tell me a joke about programming")
print(f"Setup: {result.setup}")
print(f"Punchline: {result.punchline}")
print(f"Rating: {result.rating}")

JSON Mode

For models that support JSON mode, you can also use this method:

from langchain_together import ChatTogether
from pydantic import BaseModel, Field

class Response(BaseModel):
    message: str = Field(description="The main message")
    category: str = Field(description="Category of the response")

# Use a model that supports JSON mode
llm = ChatTogether(model="Qwen/Qwen2.5-7B-Instruct-Turbo")
structured_llm = llm.with_structured_output(Response.model_json_schema(), method="json_mode")

result = structured_llm.invoke(
    "Respond with a JSON containing a message about cats and categorize it. "
    "Use the exact keys 'message' and 'category'."
)

Embeddings

from langchain_together import TogetherEmbeddings

embeddings = TogetherEmbeddings(model="intfloat/multilingual-e5-large-instruct")

Metadata

Release files for langchain-together 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-together 0.4.0
File Size Uploaded
langchain_together-0.4.0.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-together 0.4.0
File Interpreter ABI Platform
langchain_together-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.9 kB

Release files / langchain_together-0.4.0.tar.gz

Download URL langchain_together-0.4.0.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f488392ecdfeb05c5f2f8162596aff43a3b4d6a4ccf2f821d6e2602a0c5563c6
BLAKE2b-256 checksum
How to use checksums
faa92624fffd4d341454a292dde416c3d2cfbbb6354a2388a57a3d06d6ca4014
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / langchain_together-0.4.0-py3-none-any.whl

Download URL langchain_together-0.4.0-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a94a0d15b0c01844edf22941aef6fe0f065d0e31e2ba215a544b366d8a1eba89
BLAKE2b-256 checksum
How to use checksums
bb95733358fb831b73585a40dfa3c59b170772e89ce06af1c888254fbaaaf5d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page