Skip to content

Commit

Permalink
[Cookbook] Simple Function calling example with Mixtral-8x7B
Browse files Browse the repository at this point in the history
Along with some fixes to function calling using OpenAI and Anyscale modelparsers
  • Loading branch information
saqadri committed Jan 4, 2024
1 parent 33a5790 commit 35bd41d
Show file tree
Hide file tree
Showing 7 changed files with 732 additions and 37 deletions.
485 changes: 485 additions & 0 deletions cookbooks/Anyscale/Function_Calling_with_Mixtral.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cookbooks/Anyscale/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ We cover:

- Inference using Anyscale Endpoints
- Prompt chains with multiple models
- Function calling with Anyscale Endpoints & AIConfig

Read more about [AIConfig for prompt and model management](https://github.com/lastmile-ai/aiconfig).

Expand Down
91 changes: 91 additions & 0 deletions cookbooks/Anyscale/recommender.aiconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"name": "Book Finder",
"schema_version": "latest",
"metadata": {
"parameters": {},
"models": {},
"model_parsers": {
"mistralai/Mistral-7B-Instruct-v0.1": "AnyscaleEndpoint",
"mistralai/Mixtral-8x7B-Instruct-v0.1": "AnyscaleEndpoint"
}
},
"description": "Use Mixtral-8x7B function calling with Anyscale Endpoints to help recommend books",
"prompts": [
{
"name": "recommend_book",
"input": "I really enjoyed reading {{book}}, could you recommend me a book that is similar and tell me why?",
"metadata": {
"model": {
"name": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"settings": {
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"tools": [
{
"type": "function",
"function": {
"name": "list",
"description": "list queries books by genre, and returns a list of names of books",
"parameters": {
"type": "object",
"properties": {
"genre": {
"type": "string",
"enum": [
"mystery",
"nonfiction",
"memoir",
"romance",
"historical"
]
}
}
}
}
},
{
"type": "function",
"function": {
"name": "search",
"description": "search queries books by their name and returns a list of book names and their ids",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
},
{
"type": "function",
"function": {
"name": "get",
"description": "get returns a book's detailed information based on the id of the book. Note that this does not accept names, and only IDs, which you can get by using search.",
"parameters": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
}
}
}
],
"system_prompt": {
"role": "system",
"content": "Please use our book database, which you can access using functions to answer the following questions."
}
}
},
"parameters": {
"book": "To Kill a Mockingbird"
},
"remember_chat_context": true
},
"outputs": []
}
],
"$schema": "https://json.schemastore.org/aiconfig-1.0"
}
116 changes: 116 additions & 0 deletions cookbooks/Anyscale/updated.aiconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"name": "NYC Trip Planner",
"schema_version": "latest",
"metadata": {
"parameters": {},
"models": {
"meta-llama/Llama-2-7b-chat-hf": {
"model": "meta-llama/Llama-2-7b-chat-hf",
"top_p": 1,
"temperature": 1
},
"meta-llama/Llama-2-70b-chat-hf": {
"model": "meta-llama/Llama-2-70b-chat-hf",
"max_tokens": 3000,
"system_prompt": "You are an expert travel coordinator with exquisite taste."
}
},
"default_model": "meta-llama/Llama-2-7b-chat-hf",
"model_parsers": {
"meta-llama/Llama-2-7b-chat-hf": "AnyscaleEndpoint",
"meta-llama/Llama-2-13b-chat-hf": "AnyscaleEndpoint",
"Meta-Llama/Llama-Guard-7b": "AnyscaleEndpoint",
"meta-llama/Llama-2-70b-chat-hf": "AnyscaleEndpoint",
"Open-Orca/Mistral-7B-OpenOrca": "AnyscaleEndpoint",
"codellama/CodeLlama-34b-Instruct-hf": "AnyscaleEndpoint",
"HuggingFaceH4/zephyr-7b-beta": "AnyscaleEndpoint",
"mistralai/Mistral-7B-Instruct-v0.1": "AnyscaleEndpoint",
"mistralai/Mixtral-8x7B-Instruct-v0.1": "AnyscaleEndpoint",
"thenlper/gte-large": "AnyscaleEndpoint"
}
},
"description": "Intrepid explorer with ChatGPT and AIConfig",
"prompts": [
{
"name": "get_activities",
"input": "Tell me 10 fun attractions to do in NYC.",
"outputs": [
{
"output_type": "execute_result",
"execution_count": 0,
"data": {
"role": "assistant",
"content": " Sure! Here are 10 fun attractions to do in New York City:\n\n1. Visit the Statue of Liberty and Ellis Island: Take a ferry to Liberty Island to see the iconic Statue of Liberty up close and learn about its history at the nearby Ellis Island Immigration Museum.\n2. Explore the Metropolitan Museum of Art: With over 2 million works of art spanning 5,000 years of history, the Met is one of the world's largest and most comprehensive art museums.\n3. Walk across the Brooklyn Bridge: Take a stroll across this iconic bridge for spectacular views of the Manhattan skyline, the East River, and Brooklyn.\n4. See a Broadway show: New York City is home to dozens of world-class theaters and performances, including Tony-award winning shows like Hamilton, Wicked, and The Lion King.\n5. Visit Central Park: This 843-acre green oasis in the middle of Manhattan offers a peaceful escape from the hustle and bustle of the city, with plenty of walking paths, lakes, and monuments to explore.\n6. Go shopping on Fifth Avenue: From high-end designer boutiques to exclusive department stores, Fifth Avenue is the ultimate shopping destination in NYC.\n7. Visit the 9/11 Memorial & Museum: A poignant tribute to the victims of the 9/11 attacks, the memorial features two reflecting pools surrounded by the names of those who were killed, as well as a museum with artifacts and stories from that fateful day.\n8. Take a food tour: New York City is a food lover's paradise, with a diverse array of cuisines and dining options. Take a guided tour to sample some of the city's best foods, from classic pizza to artisanal doughnuts.\n9. Visit the Museum of Modern Art (MoMA): With a collection of over 200,000 works of art, MoMA is one of the world's premier modern art museums, featuring pieces by artists like Picasso, Warhol, and Pollock.\n10. Take a helicopter tour of NYC: For a unique perspective on the city, take a helicopter tour of New York City's iconic skyline and landmarks. You'll soar over the Hudson River and get a bird's-eye view of Manhattan's towering skyscrapers, bridges, and parks.\n\nThese are just a few of the many fun attractions to explore in New York City. Depending on your interests, there are countless other activities and experiences to enjoy in the city that never sleeps!"
},
"metadata": {
"finish_reason": "stop"
}
}
]
},
{
"name": "gen_itinerary",
"input": "Generate an itinerary ordered by {{order_by}} for these activities: {{get_activities.output}}.",
"metadata": {
"model": {
"name": "AnyscaleEndpoint",
"settings": {
"model": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"max_tokens": 3000,
"system_prompt": "You are an expert travel coordinator with exquisite taste."
}
},
"parameters": {
"order_by": "geographic location"
}
},
"outputs": [
{
"output_type": "execute_result",
"execution_count": 0,
"data": {
"role": "assistant",
"content": " 1. Private guided tour of the Louvre Museum (2-3 hours)\nBegin your journey with a private guided tour of the Louvre Museum, the world's largest art museum and a historic monument in Paris. This tour will allow you to explore the museum's most famous works, including the Mona Lisa, the Winged Victory of Samothrace, and the Venus de Milo, with a knowledgeable guide who can provide context and insight into the artwork.\n\n2. Champagne tasting at a prestigious champagne house in Reims (2 hours)\nNext, head to Reims, a city in the Champagne region of France, for a private champagne tasting at a prestigious champagne house such as Taittinger, Veuve Clicquot, or Ruinart. You'll learn about the history and production methods of champagne, as well as have the opportunity to taste a selection of their finest bubbly.\n\n3. Lunch at a Michelin-starred restaurant in Reims (1.5-2 hours)\nTreat yourself to lunch at one of Reims' Michelin-starred restaurants, such as Le Foch, where you can sample local specialties such as Jambon de Reims, or Reims ham, and indulge in expertly prepared dishes made with fresh, seasonal ingredients.\n\n4. Private guided tour of the Palace of Versailles (3-4 hours)\nNo trip to France would be complete without a visit to the Palace of Versailles, the opulent palace and gardens of the French monarchy. A private guided tour will allow you to explore the palace's grand halls, ornate apartments, and incomparable art collections, as well as stroll through the meticulously manicured gardens and fountains.\n\n5. Seine River dinner cruise (2-3 hours)\nEnd your day with a romantic Seine River dinner cruise, where you'll float past some of Paris' most iconic landmarks, including the Eiffel Tower, Notre-Dame Cathedral, and the Louvre Museum, all while enjoying a delicious meal and live music.\n\n6. Private guided tour of Montmartre and Sacr\u00e9-Coeur Basilica (2-3 hours)\nBegin your second day with a private guided tour of Montmartre, the historic and artistic neighborhood atop a hill in Paris. This tour will take you through the charming streets of Montmartre, past the Moulin Rouge and the Place du Tertre, to the iconic white-domed Basilica of Sacr\u00e9-Coeur.\n\n7. Cooking class at a professional cooking school in Paris (3-4 hours)\nNext, immerse yourself in French culinary traditions with a private cooking class at a professional cooking school. You'll learn how to prepare classic French dishes such as coq au vin, ratatouille, or tarte tatin, and have the opportunity to enjoy the fruits of your labor for lunch.\n\n8. Private guided tour of the Mus\u00e9e d'Orsay (2-3 hours)\nEnd your trip with a private guided tour of the Mus\u00e9e d'Orsay, a museum located in a former railway station, which houses a vast collection of Impressionist and Post-Impressionist art. This tour will allow you to see works by artists such as Monet, Renoir, Degas, and Van Gogh, and gain a deeper understanding of the artistic movements that defined this period."
},
"metadata": {
"finish_reason": "stop"
}
}
]
},
{
"name": "gen_packing_list",
"input": "What should I bring to {{location}}?",
"metadata": {
"model": {
"name": "gpt-3.5-turbo",
"settings": {
"model": "gpt-3.5-turbo",
"system_prompt": {
"role": "system",
"content": "You provide a bulleted list of items to pack for a week long trip."
}
}
},
"parameters": {
"location": "nyc"
},
"remember_chat_context": true
},
"outputs": [
{
"output_type": "execute_result",
"execution_count": 0,
"data": {
"content": "- Clothes for a week, including underwear, socks, and sleepwear\n- Outerwear appropriate for the weather (jacket, coat, or sweater)\n- Comfortable shoes for walking\n- Toiletries (toothbrush, toothpaste, shampoo, conditioner, soap, etc.)\n- Medications, if needed\n- Phone charger and other electronic accessories\n- Personal identification and travel documents (ID, passport, visa, etc.)\n- Wallet with cash and/or credit cards\n- Travel-sized umbrella or raincoat, depending on the weather forecast\n- Snacks and water bottle for the journey\n- Any necessary electronics or gadgets (camera, laptop, etc.)\n- Entertainment options (books, magazines, headphones, etc.)\n- Travel adapter, if necessary\n- Maps or guidebook of NYC\n- Sunscreen and sunglasses, if visiting during summer or sunny weather\n- Light backpack or day bag for touring around the city\n- Any special items specific to your needs or interests (art supplies, workout gear, etc.)",
"role": "assistant"
},
"metadata": {
"finish_reason": "stop"
}
}
]
}
],
"$schema": "https://json.schemastore.org/aiconfig-1.0"
}
31 changes: 16 additions & 15 deletions python/src/aiconfig/default_parsers/anyscale_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Prompt,
ToolCallData,
)
from aiconfig.util.config_utils import get_api_key_from_environment

from .openai import OpenAIInference

Expand Down Expand Up @@ -244,21 +243,23 @@ def build_output_data(
if message.get("content") is not None:
output_data = message.get("content") # string
elif message.get("tool_calls") is not None:
tool_calls = [
ToolCallData(
id=item.id,
function=FunctionCallData(
arguments=item.function.arguments,
name=item.function.name,
),
type="function",
tool_calls = []
for item in message.get("tool_calls"):
function = item.get("function")
if function is None:
continue

tool_calls.append(
ToolCallData(
id=item.get("id"),
function=FunctionCallData(
arguments=function.get("arguments"),
name=function.get("name"),
),
type=item.get("type") if not None else "function",
)
)
for item in message.get("tool_calls")
# It's possible that ChatCompletionMessageToolCall may
# support more than just function calls in the future
# so filter out other types
if item.type == "function"
]

output_data = OutputDataWithToolCallsValue(
kind="tool_calls",
value=tool_calls,
Expand Down
37 changes: 21 additions & 16 deletions python/src/aiconfig/default_parsers/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ def get_output_text(
if isinstance(output_data.value, str):
return output_data.value
# If we get here that means it must be of kind tool_calls
return json.dumps(output_data.value, indent=2)

return output_data.model_dump_json(exclude_none=True, indent=2)
# Doing this to be backwards-compatible with old output format
# where we used to save the ChatCompletionMessage in output.data
if isinstance(output_data, ChatCompletionMessage):
Expand Down Expand Up @@ -435,6 +434,8 @@ def refine_chat_completion_params(model_settings):
"stop",
"stream",
"temperature",
"tools",
"tool_choice",
"top_p",
"user",
}
Expand Down Expand Up @@ -539,21 +540,25 @@ def build_output_data(
if message.get("content") is not None:
output_data = message.get("content") # string
elif message.get("tool_calls") is not None:
tool_calls = [
ToolCallData(
id=item.id,
function=FunctionCallData(
arguments=item.function.arguments,
name=item.function.name,
),
type="function",
tool_calls = []
for item in message.get("tool_calls"):
function = item.get("function")
if function is None:
# It's possible that ChatCompletionMessageToolCall may
# support more than just function calls in the future
# so filter out other types of tool calls for now
continue

tool_calls.append(
ToolCallData(
id=item.get("id"),
function=FunctionCallData(
arguments=function.get("arguments"),
name=function.get("name"),
),
type=item.get("type") if not None else "function",
)
)
for item in message.get("tool_calls")
# It's possible that ChatCompletionMessageToolCall may
# support more than just function calls in the future
# so filter out other types
if item.type == "function"
]
output_data = OutputDataWithToolCallsValue(
kind="tool_calls",
value=tool_calls,
Expand Down
8 changes: 2 additions & 6 deletions python/src/aiconfig/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,13 +853,9 @@ def add_outputs(self, prompt_name: str, outputs: List[Output], overwrite: bool =
"""
prompt = self.get_prompt(prompt_name)
if not prompt:
raise IndexError(
f"Cannot add outputs. Prompt '{prompt_name}' not found in config."
)
raise IndexError(f"Cannot add outputs. Prompt '{prompt_name}' not found in config.")
if not outputs:
raise ValueError(
f"Cannot add outputs. No outputs provided for prompt '{prompt_name}'."
)
raise ValueError(f"Cannot add outputs. No outputs provided for prompt '{prompt_name}'.")
if overwrite:
prompt.outputs = outputs
else:
Expand Down

0 comments on commit 35bd41d

Please sign in to comment.