最近使用Gemini的用户越来越多。国内Gemini的资料相对较少,很多用户遇到问题很难快速找到资料,所以打算记录一下Gemini的基本使用,方便查找。
本系列假定已对GCP(Google Cloud Platform)有基础使用经验并可以科学上网。
GCP环境准备
使用Gemini,首先需要一个GCP项目,登录GCP Console(cloud.google.com)并记录项目的相关信息(Project Num和Project ID):
如下进入Vertex AI的Gemini界面:
如果是该项目第一次使用Vertex AI,会提示激活API:
激活后点击左侧Freeform,即可输入Prompt使用Genimi:
开发环境准备
当然,首先得要有IDE,下边以最常用的VS Code为例。Python要求3.8以上版本,为了与其他环境隔离,建议创建venv,并在VS Code安装Python解释器方便调试:
安装GCP AI的库:
1
| pip install google-cloud-aiplatform
|
现在复制Gemini Sample Code测试是否可以正常运行。在GCP Console的Gemini界面右上角点击Get Code,可以看到对应的Sample Code:
复制到IDE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import base64 import vertexai from vertexai.generative_models import GenerativeModel, Part, SafetySetting
def generate(): vertexai.init(project="ai-demo-440003", location="us-central1") model = GenerativeModel( "gemini-1.5-flash-002", ) responses = model.generate_content( ["""Please tell me how to lear python"""], generation_config=generation_config, safety_settings=safety_settings, stream=True, )
for response in responses: print(response.text, end="")
generation_config = { "max_output_tokens": 8192, "temperature": 1, "top_p": 0.95, }
safety_settings = [ SafetySetting( category=SafetySetting.HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold=SafetySetting.HarmBlockThreshold.OFF ), SafetySetting( category=SafetySetting.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold=SafetySetting.HarmBlockThreshold.OFF ), SafetySetting( category=SafetySetting.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold=SafetySetting.HarmBlockThreshold.OFF ), SafetySetting( category=SafetySetting.HarmCategory.HARM_CATEGORY_HARASSMENT, threshold=SafetySetting.HarmBlockThreshold.OFF ), ]
generate()
|
运行,出现错误:
这个错误的原因是,运行环境未找到用作授权的service account。
这是是Gemini和其他LLM差异较大的一点。其他LLM一般通过API Key认证,很多用户都在这一步不知道如何处理。下边就看看操作流程:
首先还是要在GCP Console进入Service Account的管理界面:
点击Create Service Account,首先给创建的Service Account一个名字:
第二步,因为要通过这个Service Account使用Gemini,所以需要赋予相应权限(Vertex AI User):
点击Done创建完成。
下一步,需要给Service Account创建一个Key,这个Key具备Service Account权限,用作开发环境鉴权。在Service Account列表界面点击刚创建的Service Account,选择KEYS页面,并点击ADD KEY:
选择Create new key,创建一个JSON格式的key:
创建完成,私钥会自动下载到本地。将key上传到开发环境并记录其路径,然后修改Sample Code如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import base64 import vertexai from vertexai.generative_models import GenerativeModel, Part, SafetySetting from google.oauth2 import service_account #导入google认证库的service account模块
#通过service account key创建credential cred = service_account.Credentials.from_service_account_file( '/home/gcpvm/ai-demo-440003-7b8cf6bf07d5.json' )
def generate(): vertexai.init(project="ai-demo-440003", location="us-central1", credentials=cred) #在初始化环境配置credential model = GenerativeModel( "gemini-1.5-flash-002", ) responses = model.generate_content( ["""Please tell me how to lear python"""], generation_config=generation_config, safety_settings=safety_settings, stream=True, )
for response in responses: print(response.text, end="")
generation_config = { "max_output_tokens": 8192, "temperature": 1, "top_p": 0.95, }
safety_settings = [ SafetySetting( category=SafetySetting.HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold=SafetySetting.HarmBlockThreshold.OFF ), SafetySetting( category=SafetySetting.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold=SafetySetting.HarmBlockThreshold.OFF ), SafetySetting( category=SafetySetting.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold=SafetySetting.HarmBlockThreshold.OFF ), SafetySetting( category=SafetySetting.HarmCategory.HARM_CATEGORY_HARASSMENT, threshold=SafetySetting.HarmBlockThreshold.OFF ), ]
generate()
|
再次运行成功:
小结
本篇文档描述如何配置Gemini的开发环境,内容比较简单,希望对不熟悉GCP Vertex AI的朋友有所帮助。
配置过程中如果有问题,可以检查是否权限方面的配置不当。