A programming framework for agentic AI
Add Llama API OAI compatible endpoint support (#6442)
Chester Hu9d297318b54a
diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynbindex cf719e10d..4be55fe4b 100644--- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb+++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb@@ -427,6 +427,58 @@"```"]},+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Llama API (experimental)\n",+ "\n",+ "[Llama API](https://llama.developer.meta.com?utm_source=partner-autogen&utm_medium=readme) is the Meta's first party API offering. It currently offers an [OpenAI compatible endpoint](https://llama.developer.meta.com/docs/features/compatibility).\n",+ "So you can use the {py:class}`~autogen_ext.models.openai.OpenAIChatCompletionClient` with the Llama API.\n",+ "\n",+ "This endpoint fully supports the following OpenAI client library features:\n",+ "* Chat completions\n",+ "* Model selection\n",+ "* Temperature/sampling\n",+ "* Streaming\n",+ "* Image understanding\n",+ "* Structured output (JSON mode)\n",+ "* Funciton calling (tools)"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from autogen_core.models import UserMessage\n",+ "from autogen_ext.models.openai import OpenAIChatCompletionClient\n",+ "from autogen_core import Image\n",+ "from pathlib import Path\n",+ "\n",+ "# Text\n",+ "model_client = OpenAIChatCompletionClient(\n",+ " model=\"Llama-4-Scout-17B-16E-Instruct-FP8\",\n",+ " # api_key=\"LLAMA_API_KEY\"\n",+ ")\n",+ "\n",+ "response = await model_client.create([UserMessage(content=\"Write me a poem\", source=\"user\")])\n",+ "print(response)\n",+ "await model_client.close()\n",+ "\n",+ "# Image\n",+ "model_client = OpenAIChatCompletionClient(\n",+ " model=\"Llama-4-Maverick-17B-128E-Instruct-FP8\",\n",+ " # api_key=\"LLAMA_API_KEY\"\n",+ ")\n",+ "image = Image.from_file(Path(\"test.png\"))\n",+ "\n",+ "response = await model_client.create([UserMessage(content=[\"What is in this image\", image], source=\"user\")])\n",+ "print(response)\n",+ "await model_client.close()"+ ]+ },{"cell_type": "markdown","metadata": {},diff --git a/python/packages/autogen-core/src/autogen_core/models/_model_client.py b/python/packages/autogen-core/src/autogen_core/models/_model_client.pyindex dac7b4daa..7c8519ddf 100644--- a/python/packages/autogen-core/src/autogen_core/models/_model_client.py+++ b/python/packages/autogen-core/src/autogen_core/models/_model_client.py@@ -37,6 +37,10 @@ class ModelFamily:CLAUDE_3_5_HAIKU = "claude-3-5-haiku"CLAUDE_3_5_SONNET = "claude-3-5-sonnet"CLAUDE_3_7_SONNET = "claude-3-7-sonnet"+ LLAMA_3_3_8B = "llama-3.3-8b"+ LLAMA_3_3_70B = "llama-3.3-70b"+ LLAMA_4_SCOUT = "llama-4-scout"+ LLAMA_4_MAVERICK = "llama-4-maverick"CODESRAL = "codestral"OPEN_CODESRAL_MAMBA = "open-codestral-mamba"MISTRAL = "mistral"@@ -67,6 +71,11 @@ class ModelFamily:"claude-3-5-haiku","claude-3-5-sonnet","claude-3-7-sonnet",+ # llama_models+ "llama-3.3-8b",+ "llama-3.3-70b",+ "llama-4-scout",+ "llama-4-maverick",# mistral_models"codestral","open-codestral-mamba",@@ -113,6 +122,15 @@ class ModelFamily:ModelFamily.GPT_35,)+ @staticmethod+ def is_llama(family: str) -> bool:+ return family in (+ ModelFamily.LLAMA_3_3_8B,+ ModelFamily.LLAMA_3_3_70B,+ ModelFamily.LLAMA_4_SCOUT,+ ModelFamily.LLAMA_4_MAVERICK,+ )+@staticmethoddef is_mistral(family: str) -> bool:return family in (diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_message_transform.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_message_transform.pyindex f5ddc08dd..d3f1f993b 100644--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_message_transform.py+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_message_transform.py@@ -515,6 +515,11 @@ __claude_models = [model for model in total_models if ModelFamily.is_claude(mode__gemini_models = [model for model in total_models if ModelFamily.is_gemini(model)]+__llama_models = [model for model in total_models if ModelFamily.is_llama(model)]++__unknown_models = list(+ set(total_models) - set(__openai_models) - set(__claude_models) - set(__gemini_models) - set(__llama_models)+)__mistral_models = [model for model in total_models if ModelFamily.is_mistral(model)]__unknown_models = list(@@ -530,6 +535,9 @@ for model in __claude_models:for model in __gemini_models:register_transformer("openai", model, __GEMINI_TRANSFORMER_MAP)+for model in __llama_models:+ register_transformer("openai", model, __BASE_TRANSFORMER_MAP)+for model in __mistral_models:register_transformer("openai", model, __MISTRAL_TRANSFORMER_MAP)diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_model_info.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_model_info.pyindex dfaf87cf6..c5b8b0495 100644--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_model_info.py+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_model_info.py@@ -36,6 +36,11 @@ _MODEL_POINTERS = {"claude-3-5-haiku": "claude-3-5-haiku-20241022","claude-3-5-sonnet": "claude-3-5-sonnet-20241022","claude-3-7-sonnet": "claude-3-7-sonnet-20250219",+ # Llama models+ "llama-3.3-8b": "Llama-3.3-8B-Instruct",+ "llama-3.3-70b": "Llama-3.3-70B-Instruct",+ "llama-4-scout": "Llama-4-Scout-17B-16E-Instruct-FP8",+ "llama-4-maverick": "Llama-4-Maverick-17B-128E-Instruct-FP8",}_MODEL_INFO: Dict[str, ModelInfo] = {@@ -351,6 +356,38 @@ _MODEL_INFO: Dict[str, ModelInfo] = {"structured_output": False,"multiple_system_messages": True,},+ "Llama-3.3-8B-Instruct": {+ "vision": False,+ "function_calling": True,+ "json_output": True,+ "family": ModelFamily.LLAMA_3_3_8B,+ "structured_output": False,+ "multiple_system_messages": True,+ },+ "Llama-3.3-70B-Instruct": {+ "vision": False,+ "function_calling": True,+ "json_output": True,+ "family": ModelFamily.LLAMA_3_3_70B,+ "structured_output": False,+ "multiple_system_messages": True,+ },+ "Llama-4-Scout-17B-16E-Instruct-FP8": {+ "vision": True,+ "function_calling": True,+ "json_output": True,+ "family": ModelFamily.LLAMA_4_SCOUT,+ "structured_output": True,+ "multiple_system_messages": True,+ },+ "Llama-4-Maverick-17B-128E-Instruct-FP8": {+ "vision": True,+ "function_calling": True,+ "json_output": True,+ "family": ModelFamily.LLAMA_4_MAVERICK,+ "structured_output": True,+ "multiple_system_messages": True,+ },}_MODEL_TOKEN_LIMITS: Dict[str, int] = {@@ -391,10 +428,15 @@ _MODEL_TOKEN_LIMITS: Dict[str, int] = {"claude-3-5-haiku-20241022": 50000,"claude-3-5-sonnet-20241022": 40000,"claude-3-7-sonnet-20250219": 20000,+ "Llama-3.3-8B-Instruct": 128000,+ "Llama-3.3-70B-Instruct": 128000,+ "Llama-4-Scout-17B-16E-Instruct-FP8": 128000,+ "Llama-4-Maverick-17B-128E-Instruct-FP8": 128000,}GEMINI_OPENAI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/"ANTHROPIC_OPENAI_BASE_URL = "https://api.anthropic.com/v1/"+LLAMA_API_BASE_URL = "https://api.llama.com/compat/v1/"def resolve_model(model: str) -> str:diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.pyindex ffe816e59..a86db14ff 100644--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py@@ -1369,6 +1369,11 @@ class OpenAIChatCompletionClient(BaseOpenAIChatCompletionClient, Component[OpenAcopied_args["base_url"] = _model_info.ANTHROPIC_OPENAI_BASE_URLif "api_key" not in copied_args and "ANTHROPIC_API_KEY" in os.environ:copied_args["api_key"] = os.environ["ANTHROPIC_API_KEY"]+ if copied_args["model"].startswith("Llama-"):+ if "base_url" not in copied_args:+ copied_args["base_url"] = _model_info.LLAMA_API_BASE_URL+ if "api_key" not in copied_args and "LLAMA_API_KEY" in os.environ:+ copied_args["api_key"] = os.environ["LLAMA_API_KEY"]client = _openai_client_from_config(copied_args)create_args = _create_args_from_config(copied_args)