"This notebook demonstrates how to use Autogen with Gemini via Vertex AI, which enables enhanced authentication method that also supports enterprise requirements using service accounts or even a personal Google cloud account.\n",
"To use VertexAI a Google Cloud account is needed. If you do not have one yet, just sign up for a free trial [here](https://cloud.google.com).\n",
"\n",
"Login to your account at [console.cloud.google.com](https://console.cloud.google.com)\n",
"\n",
"In the next step we create a Google Cloud project, which is needed for VertexAI. The official guide for creating a project is available is [here](https://developers.google.com/workspace/guides/create-project). \n",
"\n",
"We will name our project Autogen-with-Gemini.\n",
"\n",
"### Enable Google Cloud APIs\n",
"\n",
"If you wish to use Gemini with your personal account, then creating a Google Cloud account is enough. However, if a service account is needed, then a few extra steps are needed.\n",
"\n",
"#### Enable API for Gemini\n",
" * For enabling Gemini for Google Cloud search for \"api\" and select Enabled APIs & services. \n",
" * Then click ENABLE APIS AND SERVICES. \n",
" * Search for Gemini, and select Gemini for Google Cloud. <br/> A direct link will look like this for our autogen-with-gemini project:\n",
"* For enabling Vertex AI for Google Cloud search for \"api\" and select Enabled APIs & services. \n",
"* Then click ENABLE APIS AND SERVICES. \n",
"* Search for Vertex AI, and select Vertex AI API. <br/> A direct link for our autogen-with-gemini will be: https://console.cloud.google.com/apis/library/aiplatform.googleapis.com?project=autogen-with-gemini\n",
"* Click ENABLE Vertex AI API for Google Cloud.\n",
"\n",
"### Create a Service Account\n",
"\n",
"You can find an overview of service accounts [can be found in the cloud console](https://console.cloud.google.com/iam-admin/serviceaccounts)\n",
"Next we assign the [Vertex AI User](https://cloud.google.com/vertex-ai/docs/general/access-control#aiplatform.user) for the service account. This can be done in the [Google Cloud console](https://console.cloud.google.com/iam-admin/iam?project=autogen-with-gemini) in our `autogen-with-gemini` project.<br/>\n",
"Alternatively, we can also grant the [Vertex AI User](https://cloud.google.com/vertex-ai/docs/general/access-control#aiplatform.user) role by running a command using the gcloud CLI, for example in [Cloud Shell](https://shell.cloud.google.com/cloudshell):\n",
"* Under IAM & Admin > Service Account select the newly created service accounts, and click the option \"Manage keys\" among the items. \n",
"* From the \"ADD KEY\" dropdown select \"Create new key\" and select the JSON format and click CREATE.\n",
" * The new key will be downloaded automatically. \n",
"* You can then upload the service account key file to the from where you will be running autogen. \n",
" * Please consider restricting the permissions on the key file. For example, you could run `chmod 600 autogen-with-gemini-service-account-key.json` if your keyfile is called autogen-with-gemini-service-account-key.json."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Configure Authentication\n",
"\n",
"Authentication happens using standard [Google Cloud authentication methods](https://cloud.google.com/docs/authentication), <br/> which means\n",
"that either an already active session can be reused, or by specifying the Google application credentials of a service account. <br/><br/>\n",
"Additionally, AutoGen also supports authentication using `Credentials` objects in Python with the [google-auth library](https://google-auth.readthedocs.io/), which enables even more flexibility.<br/>\n",
"For example, we can even use impersonated credentials.\n",
"The Google Cloud service account can be specified by setting the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path to the JSON key file of the service account. <br/>\n",
"\n",
"We could even just directly set the environment variable, or we can add the `\"google_application_credentials\"` key with the respective value for our model in the OAI_CONFIG_LIST.\n",
"If you are using [Cloud Shell](https://shell.cloud.google.com/cloudshell) or [Cloud Shell editor](https://shell.cloud.google.com/cloudshell/editor) in Google Cloud, <br/> then you are already authenticated. If you have the Google Cloud SDK installed locally, <br/> then you can login by running `gcloud auth application-default login` in the command line. \n",
"Detailed instructions for installing the Google Cloud SDK can be found [here](https://cloud.google.com/sdk/docs/install).\n",
"\n",
"#### Authentication with the Google Auth Library for Python\n",
"\n",
"The google-auth library supports a wide range of authentication scenarios, and you can simply pass a previously created `Credentials` object to the `llm_config`.<br/>\n",
"The [official documentation](https://google-auth.readthedocs.io/) of the Python package provides a detailed overview of the supported methods and usage examples.<br/>\n",
"If you are already authenticated, like in [Cloud Shell](https://shell.cloud.google.com/cloudshell), or after running the `gcloud auth application-default login` command in a CLI, then the `google.auth.default()` Python method will automatically return your currently active credentials."
"Configuring safety settings for VertexAI is slightly different, as we have to use the speicialized safety setting object types instead of plain strings"
"This aligns with the analytical solution. The indefinite integral of x^2 is (x^3)/3. Evaluating this from 0 to 1 gives us (1^3)/3 - (0^3)/3 = 1/3 = 0.33333...\n",
"The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is a path to our service account JSON keyfile, as described in the [Use Service Account Keyfile](#use_svc_keyfile) section above.<br/>\n",
" The image describes a conversational agent that is able to have a conversation with a human user. The agent can be customized to the user's preferences. The conversation can be in form of a joint chat or hierarchical chat.\n",
"ChatResult(chat_id=None, chat_history=[{'content': 'Describe what is in this image?\\n<img https://github.com/microsoft/autogen/blob/main/website/static/img/autogen_agentchat.png?raw=true>.', 'role': 'assistant'}, {'content': \" The image describes a conversational agent that is able to have a conversation with a human user. The agent can be customized to the user's preferences. The conversation can be in form of a joint chat or hierarchical chat.\", 'role': 'user'}], summary=\" The image describes a conversational agent that is able to have a conversation with a human user. The agent can be customized to the user's preferences. The conversation can be in form of a joint chat or hierarchical chat.\", cost={'usage_including_cached_inference': {'total_cost': 0.0001995, 'gemini-pro-vision': {'cost': 0.0001995, 'prompt_tokens': 267, 'completion_tokens': 44, 'total_tokens': 311}}, 'usage_excluding_cached_inference': {'total_cost': 0}}, human_input=[])"
"# Use Gemini via the OpenAI Library in Autogen\n",
"Using Gemini via the OpenAI library is also possible once you are already authenticated. <br/>\n",
"Run `gcloud auth application-default login` to set up application default credentials locally for the example below.<br/>\n",
"Also set the Google cloud project on the CLI if you have not done so far: <br/>\n",
"```bash\n",
"gcloud config set project autogen-with-gemini\n",
"```\n",
"The prerequisites are essentially the same as in the example above.<br/>\n",
"\n",
"You can read more on the topic in the [official Google docs](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/call-gemini-using-openai-library).\n",
"<br/> A list of currently supported models can also be found in the [docs](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/call-gemini-using-openai-library#supported_models)\n",
"<br/>\n",
"<br/>\n",
"Note, that you will need to refresh your token regularly, by default every 1 hour."
" This function calculates the definite integral of x^3 from a to b.\n",
"\n",
" Args:\n",
" a: The lower limit of integration.\n",
" b: The upper limit of integration.\n",
"\n",
" Returns:\n",
" The value of the definite integral.\n",
" \"\"\"\n",
" return (b**4 - a**4) / 4\n",
"\n",
"# Calculate the integral of x^3 from 0 to 10\n",
"result = integrate_x_cubed(0, 10)\n",
"\n",
"# Print the result\n",
"print(result)\n",
"```\n",
"\n",
"This script defines a function `integrate_x_cubed` that takes the lower and upper limits of integration as arguments and returns the definite integral of x^3 using the power rule of integration. The script then calls this function with the limits 0 and 10 and prints the result.\n",
"\n",
"Execute the script `python integral.py`, you should get the result: `2500.0`.\n",