Skip to main content

oai.gemini

创建一个兼容 OpenAI Gemini 功能的客户端。

示例

llm_config={
"config_list": [
{
"api_type": "google",
"model": "gemini-pro",
"api_key": os.environ.get("GOOGLE_API_KEY"),
"safety_settings": [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_ONLY_HIGH"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_ONLY_HIGH"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_ONLY_HIGH"}
]
}
],
"top_p": 0.5,
"max_tokens": 2048,
"temperature": 1.0,
"top_k": 5
}

agent = autogen.AssistantAgent("my_agent", llm_config=llm_config)

资源:
- [https://ai.google.dev/docs](https://ai.google.dev/docs)
- [https://cloud.google.com/vertex-ai/docs/generative-ai/migrate-from-azure](https://cloud.google.com/vertex-ai/docs/generative-ai/migrate-from-azure)
- [https://blog.google/technology/ai/google-gemini-pro-imagen-duet-ai-update/](https://blog.google/technology/ai/google-gemini-pro-imagen-duet-ai-update/)
- [https://ai.google.dev/api/python/google/generativeai/ChatSession](https://ai.google.dev/api/python/google/generativeai/ChatSession)

GeminiClient

class GeminiClient()

Google Gemini API 的客户端。

请访问此页面了解 Gemini 在 AutoGen 中的集成路线图。

__init__

def __init__(**kwargs)

使用 LLM 配置中的 api_key 进行身份验证(也可以指定 GOOGLE_API_KEY 环境变量), 或者按照 Google Cloud 中 VertexAI 的 Google 身份验证机制进行身份验证(如果未指定 api_key), 也可以将 project_id 和 location 作为参数传递。还可以使用服务账号密钥文件。 如果未传递服务账号密钥文件或 api_key,则将使用默认凭据,如果用户已经进行了身份验证,例如在 Google Cloud Shell 中,可能是个人账号。

参数

  • api_key str - 使用 Gemini 的 API 密钥。
  • google_application_credentials str - 服务账号的 JSON 密钥文件路径。 或者,可以设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量来代替使用此参数。
  • project_id str - Google Cloud 项目 ID,仅在未指定 API 密钥时有效。
  • location str - 要使用的计算区域,如 'us-west1'。 该参数仅在未指定 API 密钥时有效。

message_retrieval

def message_retrieval(response) -> List

检索并返回响应中的字符串列表或 Choice.Message 列表。

注意:如果返回的是 Choice.Message 列表,它当前需要包含 OpenAI 的 ChatCompletion Message 对象的字段, 因为目前在代码库的其他部分中,这是函数或工具调用所期望的,除非使用自定义代理。

get_usage

@staticmethod
def get_usage(response) -> Dict

使用 RESPONSE_USAGE_KEYS 返回响应的使用情况摘要。