Skip to main content
Together.AI is a cloud based platform serving many open-weight LLMs such as Google’s Gemma, Meta’s Llama 2/3, Qwen, Mistral.AI’s Mistral/Mixtral, and NousResearch’s Hermes models. Although AG2 can be used with Together.AI’s API directly by changing the base_url to their url, it does not cater for some differences between messaging and it is recommended to use the Together.AI Client class as shown in this notebook. You will need a Together.AI account and create an API key. See their website for further details.

Features

When using this client class, messages are tailored to accommodate the specific requirements of Together.AI’s API and provide native support for function/tool calling, token usage, and accurate costs (as of June 2024).

Getting started

First, you need to install the AG2 package to use AG2 with the Together.AI extra.
If you have been using autogen or ag2, all you need to do is upgrade it using:
or
as autogen and ag2 are aliases for the same PyPI package.
Together.AI provides a large number of models to use, included some below. See the list of models here. See the sample OAI_CONFIG_LIST below showing how the Together.AI client class is used by specifying the api_type as together.
As an alternative to the api_key key and value in the config, you can set the environment variable TOGETHER_API_KEY to your Together.AI key. Linux/Mac:
Windows:

API parameters

The following Together.AI parameters can be added to your config. See this link for further information on their purpose, default values, and ranges.
  • max_tokens (integer)
  • temperature (float)
  • top_p (float)
  • top_k (integer)
  • repetition_penalty (float)
  • frequency_penalty (float)
  • presence_penalty (float)
  • min_p (float)
  • safety_model (string - moderation models here)
Example:

Two-Agent Coding Example

In this example, we run a two-agent chat with an AssistantAgent (primarily a coding agent) to generate code to count the number of prime numbers between 1 and 10,000 and then it will be executed. We’ll use Mistral’s Mixtral-8x7B instruct model which is suitable for coding.

Tool Call Example

In this example, instead of writing code, we will have two agents playing chess against each other using tool calling to make moves. Important: We are utilising a parameter that’s supported by certain client classes, such as this one, called hide_tools. This parameter will hide the tools from the Together.AI response creation call if tools have already been executed and this helps minimise the chance of the LLM choosing a tool when we don’t need it to. Here we are using if_all_run, indicating that we want to hide the tools if all the tools have already been run. This will apply in each nested chat, so each time a player takes a turn it will aim to run both functions and then finish with a text response so we can hand control back to the other player.
First install the chess package by running the following command:
Write the function for making a move.