Skip to content

Commit

Permalink
Merge branch 'main' into release/dotnet/0.0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleLittleCloud committed Jun 14, 2024
2 parents a98cacd + 6d4cf40 commit 9d1b538
Show file tree
Hide file tree
Showing 30 changed files with 4,639 additions and 2,338 deletions.
2 changes: 1 addition & 1 deletion OAI_CONFIG_LIST_sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Please modify the content, remove these four lines of comment and rename this file to OAI_CONFIG_LIST to run the sample code.
// If using pyautogen v0.1.x with Azure OpenAI, please replace "base_url" with "api_base" (line 13 and line 20 below). Use "pip list" to check version of pyautogen installed.
// If using pyautogen v0.1.x with Azure OpenAI, please replace "base_url" with "api_base" (line 14 and line 21 below). Use "pip list" to check version of pyautogen installed.
//
// NOTE: This configuration lists GPT-4 as the default model, as this represents our current recommendation, and is known to work well with AutoGen. If you use a model other than GPT-4, you may need to revise various system prompts (especially if using weaker models like GPT-3.5-turbo). Moreover, if you use models other than those hosted by OpenAI or Azure, you may incur additional risks related to alignment and safety. Proceed with caution if updating this default.
[
Expand Down
694 changes: 353 additions & 341 deletions autogen/agentchat/contrib/agent_builder.py

Large diffs are not rendered by default.

31 changes: 28 additions & 3 deletions autogen/oai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,10 @@ def cost(self, response: Union[ChatCompletion, Completion]) -> float:
"""Calculate the cost of the response."""
model = response.model
if model not in OAI_PRICE1K:
# TODO: add logging to warn that the model is not found
logger.debug(f"Model {model} is not found. The cost will be 0.", exc_info=True)
# log warning that the model is not found
logger.warning(
f'Model {model} is not found. The cost will be 0. In your config_list, add field {{"price" : [prompt_price_per_1k, completion_token_price_per_1k]}} for customized pricing.'
)
return 0

n_input_tokens = response.usage.prompt_tokens if response.usage is not None else 0 # type: ignore [union-attr]
Expand Down Expand Up @@ -328,6 +330,7 @@ class OpenAIWrapper:
"api_version",
"api_type",
"tags",
"price",
}

openai_kwargs = set(inspect.getfullargspec(OpenAI.__init__).kwonlyargs)
Expand Down Expand Up @@ -592,6 +595,14 @@ def yes_or_no_filter(context, response):
filter_func = extra_kwargs.get("filter_func")
context = extra_kwargs.get("context")
agent = extra_kwargs.get("agent")
price = extra_kwargs.get("price", None)
if isinstance(price, list):
price = tuple(price)
elif isinstance(price, float) or isinstance(price, int):
logger.warning(
"Input price is a float/int. Using the same price for prompt and completion tokens. Use a list/tuple if prompt and completion token prices are different."
)
price = (price, price)

total_usage = None
actual_usage = None
Expand Down Expand Up @@ -678,7 +689,10 @@ def yes_or_no_filter(context, response):
raise
else:
# add cost calculation before caching no matter filter is passed or not
response.cost = client.cost(response)
if price is not None:
response.cost = self._cost_with_customized_price(response, price)
else:
response.cost = client.cost(response)
actual_usage = client.get_usage(response)
total_usage = actual_usage.copy() if actual_usage is not None else total_usage
self._update_usage(actual_usage=actual_usage, total_usage=total_usage)
Expand Down Expand Up @@ -712,6 +726,17 @@ def yes_or_no_filter(context, response):
continue # filter is not passed; try the next config
raise RuntimeError("Should not reach here.")

@staticmethod
def _cost_with_customized_price(
response: ModelClient.ModelClientResponseProtocol, price_1k: Tuple[float, float]
) -> None:
"""If a customized cost is passed, overwrite the cost in the response."""
n_input_tokens = response.usage.prompt_tokens if response.usage is not None else 0 # type: ignore [union-attr]
n_output_tokens = response.usage.completion_tokens if response.usage is not None else 0 # type: ignore [union-attr]
if n_output_tokens is None:
n_output_tokens = 0
return n_input_tokens * price_1k[0] + n_output_tokens * price_1k[1]

@staticmethod
def _update_dict_from_chunk(chunk: BaseModel, d: Dict[str, Any], field: str) -> int:
"""Update the dict from the chunk.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Example13_OpenAIAgent_JsonMode
public static async Task RunAsync()
{
#region create_agent
var config = LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo(deployName: "gpt-35-turbo-0125"); // json mode only works with 0125 and later model.
var config = LLMConfiguration.GetAzureOpenAIGPT3_5_Turbo(deployName: "gpt-35-turbo"); // json mode only works with 0125 and later model.
var apiKey = config.ApiKey;
var endPoint = new Uri(config.Endpoint);

Expand Down
4 changes: 2 additions & 2 deletions dotnet/sample/AutoGen.BasicSamples/LLMConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public static OpenAIConfig GetOpenAIGPT4()
return new OpenAIConfig(openAIKey, modelId);
}

public static AzureOpenAIConfig GetAzureOpenAIGPT3_5_Turbo(string deployName = "gpt-35-turbo-16k")
public static AzureOpenAIConfig GetAzureOpenAIGPT3_5_Turbo(string? deployName = null)
{
var azureOpenAIKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");

deployName = deployName ?? Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
return new AzureOpenAIConfig(endpoint, deployName, azureOpenAIKey);
}

Expand Down
13 changes: 6 additions & 7 deletions dotnet/test/AutoGen.OpenAI.Tests/MathClassTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,15 @@ the number of resolved question is {correctAnswerCount}
}


[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
public async Task OpenAIAgentMathChatTestAsync()
{
var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new ArgumentException("AZURE_OPENAI_API_KEY is not set");
var endPoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new ArgumentException("AZURE_OPENAI_ENDPOINT is not set");

var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new ArgumentException("AZURE_OPENAI_DEPLOY_NAME is not set");
var openaiClient = new OpenAIClient(new Uri(endPoint), new Azure.AzureKeyCredential(key));
var model = "gpt-35-turbo-16k";
var teacher = await CreateTeacherAgentAsync(openaiClient, model);
var student = await CreateStudentAssistantAgentAsync(openaiClient, model);
var teacher = await CreateTeacherAgentAsync(openaiClient, deployName);
var student = await CreateStudentAssistantAgentAsync(openaiClient, deployName);

var adminFunctionMiddleware = new FunctionCallMiddleware(
functions: [this.UpdateProgressFunctionContract],
Expand All @@ -115,7 +114,7 @@ public async Task OpenAIAgentMathChatTestAsync()
});
var admin = new OpenAIChatAgent(
openAIClient: openaiClient,
modelName: model,
modelName: deployName,
name: "Admin",
systemMessage: $@"You are admin. You update progress after each question is answered.")
.RegisterMessageConnector()
Expand All @@ -124,7 +123,7 @@ public async Task OpenAIAgentMathChatTestAsync()

var groupAdmin = new OpenAIChatAgent(
openAIClient: openaiClient,
modelName: model,
modelName: deployName,
name: "GroupAdmin",
systemMessage: "You are group admin. You manage the group chat.")
.RegisterMessageConnector()
Expand Down
20 changes: 12 additions & 8 deletions dotnet/test/AutoGen.OpenAI.Tests/OpenAIChatAgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ public async Task<string> GetWeatherAsync(string location)
return $"The weather in {location} is sunny.";
}

[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
public async Task BasicConversationTestAsync()
{
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
var openaiClient = new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(key));
var openAIChatAgent = new OpenAIChatAgent(
openAIClient: openaiClient,
name: "assistant",
modelName: "gpt-35-turbo-16k");
modelName: deployName);

// By default, OpenAIChatClient supports the following message types
// - IMessage<ChatRequestMessage>
Expand All @@ -56,16 +57,17 @@ public async Task BasicConversationTestAsync()
}
}

[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
public async Task OpenAIChatMessageContentConnectorTestAsync()
{
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
var openaiClient = new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(key));
var openAIChatAgent = new OpenAIChatAgent(
openAIClient: openaiClient,
name: "assistant",
modelName: "gpt-35-turbo-16k");
modelName: deployName);

MiddlewareStreamingAgent<OpenAIChatAgent> assistant = openAIChatAgent
.RegisterMessageConnector();
Expand Down Expand Up @@ -102,16 +104,17 @@ public async Task OpenAIChatMessageContentConnectorTestAsync()
}
}

[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
public async Task OpenAIChatAgentToolCallTestAsync()
{
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
var openaiClient = new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(key));
var openAIChatAgent = new OpenAIChatAgent(
openAIClient: openaiClient,
name: "assistant",
modelName: "gpt-35-turbo-16k");
modelName: deployName);

var functionCallMiddleware = new FunctionCallMiddleware(
functions: [this.GetWeatherAsyncFunctionContract]);
Expand Down Expand Up @@ -170,16 +173,17 @@ public async Task OpenAIChatAgentToolCallTestAsync()
}
}

[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
public async Task OpenAIChatAgentToolCallInvokingTestAsync()
{
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
var openaiClient = new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(key));
var openAIChatAgent = new OpenAIChatAgent(
openAIClient: openaiClient,
name: "assistant",
modelName: "gpt-35-turbo-16k");
modelName: deployName);

var functionCallMiddleware = new FunctionCallMiddleware(
functions: [this.GetWeatherAsyncFunctionContract],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ namespace AutoGen.SemanticKernel.Tests;

public class KernelFunctionMiddlewareTests
{
[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
public async Task ItRegisterKernelFunctionMiddlewareFromTestPluginTests()
{
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
var openaiClient = new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(key));

var kernel = new Kernel();
var plugin = kernel.ImportPluginFromType<TestPlugin>();
var kernelFunctionMiddleware = new KernelPluginMiddleware(kernel, plugin);

var agent = new OpenAIChatAgent(openaiClient, "assistant", modelName: "gpt-35-turbo-16k")
var agent = new OpenAIChatAgent(openaiClient, "assistant", modelName: deployName)
.RegisterMessageConnector()
.RegisterMiddleware(kernelFunctionMiddleware);

Expand Down Expand Up @@ -56,11 +57,12 @@ public async Task ItRegisterKernelFunctionMiddlewareFromTestPluginTests()
}
}

[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT")]
[ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
public async Task ItRegisterKernelFunctionMiddlewareFromMethodTests()
{
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
var openaiClient = new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(key));

var kernel = new Kernel();
Expand All @@ -69,7 +71,7 @@ public async Task ItRegisterKernelFunctionMiddlewareFromMethodTests()
var plugin = kernel.ImportPluginFromFunctions("plugin", [getWeatherMethod, createPersonObjectMethod]);
var kernelFunctionMiddleware = new KernelPluginMiddleware(kernel, plugin);

var agent = new OpenAIChatAgent(openaiClient, "assistant", modelName: "gpt-35-turbo-16k")
var agent = new OpenAIChatAgent(openaiClient, "assistant", modelName: deployName)
.RegisterMessageConnector()
.RegisterMiddleware(kernelFunctionMiddleware);

Expand Down
Loading

0 comments on commit 9d1b538

Please sign in to comment.