Google Gemini 1.0 对接 - Python demo

安装python SDK

pip install -q -U google-generativeai

使用

image.png

gemini,内容生成简单demo

"""
At the command line, only need to run once to install the package via pip:

$ pip install google-generativeai
"""

import google.generativeai as genai

genai.configure(api_key="填写自己申请的key")

# Set up the model
generation_config = {
  "temperature": 0.9,
  "top_p": 1,
  "top_k": 1,
  "max_output_tokens": 2048,
}

safety_settings = [
  {
    "category": "HARM_CATEGORY_HARASSMENT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_HATE_SPEECH",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
  {
    "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
    "threshold": "BLOCK_MEDIUM_AND_ABOVE"
  },
]

model = genai.GenerativeModel(model_name="gemini-pro",
                              generation_config=generation_config,
                              safety_settings=safety_settings)

#输入提示
prompt_parts = [ "帮我写一篇XXXX", ] 
#输出回答
response = model.generate_content(prompt_parts) 
print(response.text)



转载请说明出处 内容投诉内容投诉
趣谈网 » Google Gemini 1.0 对接 - Python demo
您需要 登录账户 后才能发表评论

发表评论

欢迎 访客 发表评论