Merge pull request #1101 from IlumCI/OpenAIFunctionCaller-Removal

[BUG-LiteLLM][Removed OpenAIFunctionCaller completely, and replaced with LiteLLM]
pull/966/merge
Kye Gomez 1 week ago committed by GitHub
commit a762c79d6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,6 +1,6 @@
from dotenv import load_dotenv
from swarms import Agent
from swarms.utils.function_caller_model import OpenAIFunctionCaller
from swarms.utils.litellm_wrapper import LiteLLM
from pydantic import BaseModel, Field
from swarms.structs.conversation import Conversation
@ -180,8 +180,9 @@ Maintain a warm, friendly, and authentic presence while ensuring all interaction
# Initialize Agents using swarms
########################################
model = OpenAIFunctionCaller(
base_model=CallLog,
model = LiteLLM(
model_name="gpt-4.1",
response_format=CallLog,
system_prompt=MASTER_AGENT_SYS_PROMPT,
)
@ -192,7 +193,7 @@ counselor_agent = Agent(
agent_description="Provides empathetic and effective college counseling and guidance.",
system_prompt=COUNSELOR_AGENT_SYS_PROMPT,
max_loops=1,
model_name="gpt-4o",
model_name="gpt-4.1",
dynamic_temperature_enabled=True,
)
@ -202,7 +203,7 @@ buddy_agent = Agent(
agent_description="Acts as a supportive, friendly companion to the student.",
system_prompt=BUDDY_AGENT_SYS_PROMPT,
max_loops=1,
model_name="gpt-4o",
model_name="gpt-4.1",
dynamic_temperature_enabled=True,
)

@ -9,7 +9,7 @@ Todo
import os
from dotenv import load_dotenv
from swarms import Agent, AgentRearrange
from swarm_models import OpenAIChat, OpenAIFunctionCaller
from swarms.utils.litellm_wrapper import LiteLLM
from pydantic import BaseModel
from typing import List
@ -31,10 +31,8 @@ load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
# Initialize the model
model = OpenAIChat(
openai_api_base="https://api.groq.com/openai/v1",
openai_api_key=api_key,
model_name="llama-3.1-70b-versatile",
model = LiteLLM(
model_name="groq/llama-3.1-70b-versatile",
temperature=0.1,
)
@ -52,11 +50,11 @@ You are a college selection final decision maker. Your role is to:
"""
function_caller = OpenAIFunctionCaller(
function_caller = LiteLLM(
model_name="gpt-4.1",
system_prompt=FINAL_AGENT_PROMPT,
openai_api_key=os.getenv("OPENAI_API_KEY"),
base_model=CollegesRecommendation,
parallel_tool_calls=True,
response_format=CollegesRecommendation,
temperature=0.1,
)
# Student Profile Analyzer Agent

@ -8,7 +8,7 @@ Todo
import os
from dotenv import load_dotenv
from swarm_models import OpenAIChat, OpenAIFunctionCaller
from swarms.utils.litellm_wrapper import LiteLLM
from pydantic import BaseModel
from typing import List
@ -30,21 +30,19 @@ load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
# Initialize the model
model = OpenAIChat(
openai_api_base="https://api.groq.com/openai/v1",
openai_api_key=api_key,
model_name="llama-3.1-70b-versatile",
model = LiteLLM(
model_name="groq/llama-3.1-70b-versatile",
temperature=0.1,
)
function_caller = OpenAIFunctionCaller(
function_caller = LiteLLM(
model_name="gpt-4.1",
system_prompt="""You are a college selection final decision maker. Your role is to:
- Balance all relevant factors and stakeholder input.
- Only return the output in the schema format.
""",
openai_api_key=os.getenv("OPENAI_API_KEY"),
base_model=CollegesRecommendation,
# parallel_tool_calls=True,
response_format=CollegesRecommendation,
temperature=0.1,
)

@ -9,7 +9,7 @@ Todo
import os
from dotenv import load_dotenv
from swarms import Agent, SequentialWorkflow
from swarm_models import OpenAIChat, OpenAIFunctionCaller
from swarms.utils.litellm_wrapper import LiteLLM
from pydantic import BaseModel
from typing import List
@ -31,10 +31,8 @@ load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
# Initialize the model
model = OpenAIChat(
openai_api_base="https://api.groq.com/openai/v1",
openai_api_key=api_key,
model_name="llama-3.1-70b-versatile",
model = LiteLLM(
model_name="groq/llama-3.1-70b-versatile",
temperature=0.1,
)
@ -52,11 +50,11 @@ You are a college selection final decision maker. Your role is to:
"""
function_caller = OpenAIFunctionCaller(
function_caller = LiteLLM(
model_name="gpt-4.1",
system_prompt=FINAL_AGENT_PROMPT,
openai_api_key=os.getenv("OPENAI_API_KEY"),
base_model=CollegesRecommendation,
parallel_tool_calls=True,
response_format=CollegesRecommendation,
temperature=0.1,
)
# Student Profile Analyzer Agent

@ -5,7 +5,7 @@ from typing import Any, Callable, Dict, Optional, Type, Union
from dotenv import load_dotenv
from pydantic import BaseModel, Field, ValidationError, create_model
from swarm_models.openai_function_caller import OpenAIFunctionCaller
from swarms.utils.litellm_wrapper import LiteLLM
class DynamicParser:
@ -216,14 +216,13 @@ Your role is to make decisions and complete tasks independently without seeking
Always respond in a strict JSON format as described below. Ensure your responses can be parsed with Python's `json.loads`:
"""
# Initialize the OpenAIFunctionCaller
model = OpenAIFunctionCaller(
# Initialize the LiteLLM
model = LiteLLM(
model_name="gpt-4.1",
system_prompt=SYSTEM_PROMPT,
max_tokens=4000,
temperature=0.9,
base_model=AgentResponse, # Pass the Pydantic schema as the base model
parallel_tool_calls=False,
openai_api_key=os.getenv("OPENAI_API_KEY"),
response_format=AgentResponse, # Pass the Pydantic schema as the response format
)
# Example usage

@ -7,7 +7,7 @@ from swarms.structs.hiearchical_swarm import (
HierarchicalSwarm,
SwarmSpec,
)
from swarms.utils.function_caller_model import OpenAIFunctionCaller
from swarms.utils.litellm_wrapper import LiteLLM
load_dotenv()
@ -15,9 +15,9 @@ load_dotenv()
# ------------------------------------------------------------------------------
# Director LLM: Responsible for orchestrating tasks among the agents
# ------------------------------------------------------------------------------
llm = OpenAIFunctionCaller(
base_model=SwarmSpec,
api_key=os.getenv("OPENAI_API_KEY"),
llm = LiteLLM(
model_name="gpt-4.1",
response_format=SwarmSpec,
system_prompt=(
"As the Director of this Hierarchical Agent Swarm, you are in charge of "
"coordinating and overseeing all tasks, ensuring that each is executed "
@ -57,7 +57,7 @@ def main():
# --------------------------------------------------------------------------
analysis_agent = Agent(
agent_name="Stock-Analysis-Agent",
model_name="gpt-4o",
model_name="gpt-4.1",
max_loops=1,
interactive=False,
streaming_on=False,

@ -7,16 +7,16 @@ from swarms.structs.hiearchical_swarm import (
HierarchicalSwarm,
SwarmSpec,
)
from swarms.utils.function_caller_model import OpenAIFunctionCaller
from swarms.utils.litellm_wrapper import LiteLLM
load_dotenv()
# ------------------------------------------------------------------------------
# Trading Director: Responsible for orchestrating tasks among multiple stock analysts
# ------------------------------------------------------------------------------
director_llm = OpenAIFunctionCaller(
base_model=SwarmSpec,
api_key=os.getenv("OPENAI_API_KEY"),
director_llm = LiteLLM(
model_name="gpt-4.1",
response_format=SwarmSpec,
system_prompt=(
"You are the Trading Director in charge of coordinating a team of specialized "
"Stock Analysts. Your responsibilities include:\n\n"
@ -51,7 +51,7 @@ def main():
# --------------------------------------------------------------------------
macro_agent = Agent(
agent_name="Macro-Economic-Analysis-Agent",
model_name="gpt-4o",
model_name="gpt-4.1",
max_loops=1,
interactive=False,
streaming_on=False,
@ -81,7 +81,7 @@ def main():
# --------------------------------------------------------------------------
sector_agent = Agent(
agent_name="Sector-Performance-Analysis-Agent",
model_name="gpt-4o",
model_name="gpt-4.1",
max_loops=1,
interactive=False,
streaming_on=False,
@ -113,7 +113,7 @@ def main():
# --------------------------------------------------------------------------
technical_agent = Agent(
agent_name="Technical-Analysis-Agent",
model_name="gpt-4o",
model_name="gpt-4.1",
max_loops=1,
interactive=False,
streaming_on=False,
@ -145,7 +145,7 @@ def main():
# --------------------------------------------------------------------------
risk_agent = Agent(
agent_name="Risk-Analysis-Agent",
model_name="gpt-4o",
model_name="gpt-4.1",
max_loops=1,
interactive=False,
streaming_on=False,

@ -75,7 +75,6 @@ numpy = "*"
litellm = "*"
httpx = "*"
mcp = "*"
openai = "*"
aiohttp = "*"
orjson = "*"
schedule = "*"

@ -24,7 +24,6 @@ httpx
aiohttp
mcp
numpy
openai
orjson
schedule
uvloop

@ -9,7 +9,7 @@ from pydantic import BaseModel, Field
from swarms.structs.agent import Agent
from swarms.structs.swarm_router import SwarmRouter
from swarms.utils.function_caller_model import OpenAIFunctionCaller
from swarms.utils.litellm_wrapper import LiteLLM
load_dotenv()
@ -162,11 +162,11 @@ class MemeAgentGenerator:
list: List of created agents
"""
logger.info("Creating agents for task")
model = OpenAIFunctionCaller(
model = LiteLLM(
model_name="gpt-4.1",
system_prompt=BOSS_SYSTEM_PROMPT,
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.1,
base_model=MemeSwarmConfig,
response_format=MemeSwarmConfig,
)
agents_dictionary = model.run(task)
@ -227,7 +227,7 @@ class MemeAgentGenerator:
agent_name=agent_name,
description=agent_description,
system_prompt=agent_system_prompt,
model_name="gpt-4o-mini",
model_name="gpt-4.1",
max_loops=max_loops,
autosave=True,
dashboard=False,

@ -4,7 +4,6 @@ from concurrent.futures import ThreadPoolExecutor
from typing import Optional
from pydantic import BaseModel, Field
from swarms.utils.function_caller_model import OpenAIFunctionCaller
from swarms.utils.any_to_str import any_to_str
from swarms.utils.formatter import formatter
from swarms.utils.litellm_wrapper import LiteLLM
@ -174,7 +173,7 @@ class ModelRouter:
temperature (float): Temperature parameter for model randomness
max_workers (int): Maximum concurrent workers for batch processing
model_output (ModelOutput): Pydantic model for structured outputs
model_caller (OpenAIFunctionCaller): Function calling interface
model_caller (LiteLLM): Function calling interface
"""
def __init__(
@ -210,11 +209,11 @@ class ModelRouter:
if self.max_workers == "auto":
self.max_workers = os.cpu_count()
self.model_caller = OpenAIFunctionCaller(
base_model=ModelOutput,
self.model_caller = LiteLLM(
model_name="gpt-4o",
response_format=ModelOutput,
temperature=self.temperature,
system_prompt=self.system_prompt,
api_key=api_key,
)
except Exception as e:
raise RuntimeError(

@ -1,123 +0,0 @@
import os
from concurrent.futures import ThreadPoolExecutor
from typing import List
from pydantic import BaseModel
from openai import OpenAI
SUPPORTED_MODELS = [
"o3-mini-2025-1-31",
"o1-2024-12-17",
"gpt-4o-mini-2024-07-18",
"gpt-4o-2024-08-06",
]
def check_api_key():
api_key = os.getenv("OPENAI_API_KEY")
if api_key is None:
raise ValueError(
"API key is not set. Please set the API key using the api_key parameter."
)
return api_key
class OpenAIFunctionCaller:
"""
A class to interact with the OpenAI API for generating text based on a system prompt and a task.
Attributes:
- system_prompt (str): The system prompt to guide the AI's response.
- api_key (str): The API key for the OpenAI service.
- temperature (float): The temperature parameter for the AI model, controlling randomness.
- base_model (BaseModel): The Pydantic model to parse the response into.
- max_tokens (int): The maximum number of tokens in the response.
- client (OpenAI): The OpenAI client instance.
"""
def __init__(
self,
system_prompt: str,
base_model: BaseModel,
api_key: str = os.getenv("OPENAI_API_KEY"),
temperature: float = 0.1,
max_tokens: int = 5000,
model_name: str = "gpt-4o-2024-08-06",
):
self.system_prompt = system_prompt
self.api_key = api_key
self.temperature = temperature
self.base_model = base_model
self.max_tokens = max_tokens
self.model_name = model_name
self.client = OpenAI(api_key=self.api_key)
def run(self, task: str):
"""
Run the OpenAI model with the system prompt and task to generate a response.
Args:
- task (str): The task to be completed.
- *args: Additional positional arguments for the OpenAI API.
- **kwargs: Additional keyword arguments for the OpenAI API.
Returns:
- BaseModel: The parsed response based on the base_model.
"""
try:
completion = self.client.beta.chat.completions.parse(
model=self.model_name,
messages=[
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": task},
],
response_format=self.base_model,
max_tokens=self.max_tokens,
temperature=self.temperature,
)
return completion.choices[0].message.parsed
except Exception as e:
print(f"There was an error: {e}")
def check_model_support(self):
# need to print the supported models
for model in SUPPORTED_MODELS:
print(model)
return SUPPORTED_MODELS
def batch_run(self, tasks: List[str]) -> List[BaseModel]:
"""
Batch run the OpenAI model with the system prompt and task to generate a response.
"""
return [self.run(task) for task in tasks]
def concurrent_run(self, tasks: List[str]) -> List[BaseModel]:
"""
Concurrent run the OpenAI model with the system prompt and task to generate a response.
"""
with ThreadPoolExecutor(max_workers=len(tasks)) as executor:
return list(executor.map(self.run, tasks))
# class TestModel(BaseModel):
# name: str
# age: int
# # Example usage
# model = OpenAIFunctionCaller(
# system_prompt="You are a helpful assistant that returns structured data about people.",
# base_model=TestModel,
# api_key=os.getenv("OPENAI_API_KEY"),
# temperature=0.7,
# max_tokens=1000
# )
# # Test with a more appropriate prompt for the TestModel schema
# response = model.run("Tell me about a person named John who is 25 years old")
# print(response)

@ -159,7 +159,7 @@ def write_markdown_report(
def create_test_agent(
name: str,
system_prompt: str = None,
model_name: str = "gpt-4o-mini",
model_name: str = "gpt-4.1",
tools: List[Callable] = None,
**kwargs,
) -> Agent:
@ -463,9 +463,7 @@ def test_spreadsheet_swarm():
def test_hierarchical_swarm():
"""Test HierarchicalSwarm structure"""
try:
from swarms.utils.function_caller_model import (
OpenAIFunctionCaller,
)
from swarms.utils.litellm_wrapper import LiteLLM
from swarms.structs.hiearchical_swarm import SwarmSpec
# Create worker agents
@ -481,9 +479,9 @@ def test_hierarchical_swarm():
]
# Create director agent with explicit knowledge of available agents
director = OpenAIFunctionCaller(
base_model=SwarmSpec,
api_key=API_KEY,
director = LiteLLM(
model_name="gpt-4.1",
response_format=SwarmSpec,
system_prompt=(
"As the Director of this Hierarchical Agent Swarm, you coordinate tasks among agents. "
"You must ONLY assign tasks to the following available agents:\n"

Loading…
Cancel
Save