Skip to content

Commit

Permalink
updated examples to include before and after autogen 0.2.3 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerprogramming committed Jan 9, 2024
1 parent e14d731 commit 04df6b5
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 61 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
### This repo will be helpful in understanding AutoGen providing examples including prompts and agents for SAAS products, how AutoGen works, and diving into the functionality.

## Current Library Versions:
- memgpt: 0.2.6
- autogen: 0.2.2
- lm studio: 0.2.10
<a href="https://github.com/microsoft/autogen/tree/main"><img src="https://img.shields.io/badge/AutoGen-0.2.3-red"/></a>
<a href="https://lmstudio.ai/"><img src="https://img.shields.io/badge/LMStudio-0.2.10-purple"/></a>
<a href="https://github.com/cpacker/MemGPT"><img src="https://img.shields.io/badge/MemGPT-0.2.11-blue"/></a>

## FFMPEG: must be installed to use Whisper AI
- MACOS: https://superuser.com/questions/624561/install-ffmpeg-on-os-x
Expand All @@ -19,7 +19,7 @@
1. **autogen_memgpt** - understanding integration of MemGPT into AutoGen as an AI Agent
2. **autogen_memgpt_lmstudio** - using a local llm to integrate MemGPT into AutoGen with a local server produced by LMStudio
3. **autogentest** - examples of basic usage of AutoGen
4. **autogen_functions** - learn how to use functions with AutoGen
4. **autogen_functions** - updated with a previous example and new example of function calling
5. **autogen_multiple_configs** - learn how to use multiple configurations in order to use multiple models with AutoGen
6. **autogen_transcribe_video** - learn how to use AutoGen and GPT to transcribe and translate a video with functions
7. **ai_agency_01_workout_plan** - create your first agency with functions and saving the output to a .txt and .csv file
Expand All @@ -38,5 +38,5 @@
- [ ] Create a Sudoku Puzzle Creator/Checker with an AI WorkForce
- [x] Create WebScraper with Puppeteer
- [x] Create AutoGen with Whisper
- [ ] Fitness Tracker with multiple models and LMStudio for LocalLLM
- [x] Fitness Tracker with multiple models and LMStudio for LocalLLM
- [x] Fitness Expert Bot with Flask Server
2 changes: 2 additions & 0 deletions autogen_functions/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OPENAI_API_KEY=sk-1111
model=gpt-4
95 changes: 45 additions & 50 deletions autogen_functions/autogen_function_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Literal

import autogen
from IPython import get_ipython

config_list = autogen.config_list_from_json(
env_or_file="OAI_CONFIG_LIST",
Expand All @@ -8,87 +9,81 @@
},
)

print(config_list)

llm_config = {
"functions": [
{
"name": "python",
"description": "run cell in ipython and return the execution result.",
"name": "currency_calculator",
"description": "Currency exchange calculator.",
"parameters": {
"type": "object",
"properties": {
"cell": {
"base_amount": {
"type": "string",
"description": "Valid Python cell to execute.",
}
},
"required": ["cell"],
},
},
{
"name": "sh",
"description": "run a shell script and return the execution result.",
"parameters": {
"type": "object",
"properties": {
"script": {
"description": "the base amount",
},
"base_currency": {
"type": "string",
"description": "Valid shell script to execute.",
}
"description": "the base currency",
},
"quote_currency": {
"type": "string",
"description": "the quote currency",
},
},
"required": ["script"],
"required": ["base_amount", "base_currency", "quote_currency"],
},
},
],
"config_list": config_list,
"timeout": 120,
"seed": 48
}
chatbot = autogen.AssistantAgent(
name="chatbot",
system_message="For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.",


def exchange_rate(base_currency, quote_currency) -> float:
if base_currency == quote_currency:
return 1.0
elif base_currency == "USD" and quote_currency == "EUR":
return 1 / 1.09
elif base_currency == "EUR" and quote_currency == "USD":
return 1.1
else:
raise ValueError(f"Unknown currencies {base_currency}, {quote_currency}")


def currency_calculator(
base_amount=0.0,
base_currency="USD",
quote_currency="EUR",
) -> str:
quote_amount = exchange_rate(base_currency, quote_currency) * float(base_amount)
return f"{quote_amount} {quote_currency}"


currency_bot = autogen.AssistantAgent(
name="currency_bot",
system_message="For currency exchange tasks, only use the functions you have been provided with. Reply TERMINATE "
"when the task is done.",
llm_config=llm_config,
)

# create a UserProxyAgent instance named "user_proxy"
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
human_input_mode="NEVER",
max_consecutive_auto_reply=10,
code_execution_config={"work_dir": "coding"},
)

# define functions according to the function description
from IPython import get_ipython


def exec_python(cell):
ipython = get_ipython()
result = ipython.run_cell(cell)
log = str(result.result)
if result.error_before_exec is not None:
log += f"\n{result.error_before_exec}"
if result.error_in_exec is not None:
log += f"\n{result.error_in_exec}"
return log


def exec_sh(script):
return user_proxy.execute_code_blocks([("sh", script)])

CurrencySymbol = Literal["USD", "EUR"]

# register the functions
user_proxy.register_function(
function_map={
"python": exec_python,
"sh": exec_sh,
"currency_calculator": currency_calculator
}
)

# start the conversation
user_proxy.initiate_chat(
chatbot,
message="Draw two agents chatting with each other with an example dialog. Don't add plt.show().",
currency_bot,
message="How much is 123.45 USD in EUR?",
)
65 changes: 65 additions & 0 deletions autogen_functions/autogen_function_example_decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import autogen
import dotenv
import os

from typing import Literal
from typing_extensions import Annotated

dotenv.load_dotenv()

config_list = [
{
"model": os.getenv("model"),
"api_key": os.getenv("OPENAI_API_KEY"),
}
]

llm_config = {
"config_list": config_list,
"timeout": 120,
}

currency_bot = autogen.AssistantAgent(
name="currency_bot",
system_message="For currency exchange tasks, only use the functions you have been provided with. Reply TERMINATE "
"when the task is done.",
llm_config=llm_config,
)

user_proxy = autogen.UserProxyAgent(
name="user_proxy",
is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
human_input_mode="NEVER",
max_consecutive_auto_reply=10,
)

CurrencySymbol = Literal["USD", "EUR"]


def exchange_rate(base_currency: CurrencySymbol, quote_currency: CurrencySymbol) -> float:
if base_currency == quote_currency:
return 1.0
elif base_currency == "USD" and quote_currency == "EUR":
return 1 / 1.09
elif base_currency == "EUR" and quote_currency == "USD":
return 1.1
else:
raise ValueError(f"Unknown currencies {base_currency}, {quote_currency}")


@user_proxy.register_for_execution()
@currency_bot.register_for_llm(description="Currency exchange calculator.")
def currency_calculator(
base_amount: Annotated[float, "Amount of currency in base_currency"],
base_currency: Annotated[CurrencySymbol, "Base currency"] = "USD",
quote_currency: Annotated[CurrencySymbol, "Quote currency"] = "EUR",
) -> str:
quote_amount = exchange_rate(base_currency, quote_currency) * base_amount
return f"{quote_amount} {quote_currency}"


# start the conversation
user_proxy.initiate_chat(
currency_bot,
message="How much is 123.45 USD in EUR?",
)
21 changes: 15 additions & 6 deletions autogen_functions/autogen_function_planner.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import autogen

config_list = autogen.config_list_from_json(
env_or_file="OAI_CONFIG_LIST",
filter_dict={
"model": ["gpt-4"]
},
)
# config_list = autogen.config_list_from_json(
# env_or_file="OAI_CONFIG_LIST",
# filter_dict={
# "model": ["gpt-4"]
# },
# )

config_list = {
"model": "NULL", # not needed
# NOTE: on versions of pyautogen < 0.2.0 use "api_base", and also uncomment "api_type"
# "api_base": "http://localhost:1234/v1",
# "api_type": "open_ai",
"base_url": "http://localhost:1234/v1", # ex. "http://127.0.0.1:5001/v1" if you are using webui, "http://localhost:1234/v1/" if you are using LM Studio
"api_key": "NULL", # not needed
}

# print(config_list)

Expand Down

0 comments on commit 04df6b5

Please sign in to comment.