Skip to main content

ChatTogether

This page will help you get started with Together AI chat models. For detailed documentation of all ChatTogether features and configurations head to the API reference.

Together AI offers an API to query 50+ leading open-source models

Overviewโ€‹

Integration detailsโ€‹

ClassPackageLocalSerializableJS supportPackage downloadsPackage latest
ChatTogetherlangchain-togetherโŒbetaโœ…PyPI - DownloadsPyPI - Version

Model featuresโ€‹

Tool callingStructured outputJSON modeImage inputAudio inputVideo inputToken-level streamingNative asyncToken usageLogprobs
โœ…โœ…โœ…โœ…โœ…โœ…โœ…โŒโœ…โœ…

Setupโ€‹

To access Together models you'll need to create a/an Together account, get an API key, and install the langchain-together integration package.

Credentialsโ€‹

Head to this page to sign up to Together and generate an API key. Once you've done this set the TOGETHER_API_KEY environment variable:

import getpass
import os

if "TOGETHER_API_KEY" not in os.environ:
os.environ["TOGETHER_API_KEY"] = getpass.getpass("Enter your Together API key: ")

If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

Installationโ€‹

The LangChain Together integration lives in the langchain-together package:

%pip install -qU langchain-together

Instantiationโ€‹

Now we can instantiate our model object and generate chat completions:

from langchain_together import ChatTogether

llm = ChatTogether(
model="meta-llama/Llama-3-70b-chat-hf",
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
# other params...
)

Invocationโ€‹

messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
AIMessage(content="J'adore la programmation.", response_metadata={'token_usage': {'completion_tokens': 9, 'prompt_tokens': 35, 'total_tokens': 44}, 'model_name': 'meta-llama/Llama-3-70b-chat-hf', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-eabcbe33-cdd8-45b8-ab0b-f90b6e7dfad8-0', usage_metadata={'input_tokens': 35, 'output_tokens': 9, 'total_tokens': 44})
print(ai_msg.content)
J'adore la programmation.

Chainingโ€‹

We can chain our model with a prompt template like so:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)

chain = prompt | llm
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
API Reference:ChatPromptTemplate
AIMessage(content='Ich liebe das Programmieren.', response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 30, 'total_tokens': 37}, 'model_name': 'meta-llama/Llama-3-70b-chat-hf', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-a249aa24-ee31-46ba-9bf9-f4eb135b0a95-0', usage_metadata={'input_tokens': 30, 'output_tokens': 7, 'total_tokens': 37})

API referenceโ€‹

For detailed documentation of all ChatTogether features and configurations head to the API reference: https://python.langchain.com/v0.2/api_reference/together/chat_models/langchain_together.chat_models.ChatTogether.html


Was this page helpful?


You can also leave detailed feedback on GitHub.