echo-e"\n\nTo see your code, visit this URL:\n \ https://github.com/${GITHUB_USERNAME}/codelab-genai/blob/main/src/main/java/com/example/demo/DemoApplication.java \n\n"
echo-e"\n\nOnce the build finishes, visit your live application:\n \ "$(\gcloudrunserviceslist|\grepcodelab-genai|\awk'/URL/{print$2}'|\head-1\)" \n\n"
packagecom.example.demo;importjava.io.IOException;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importcom.google.cloud.vertexai.VertexAI;importcom.google.cloud.vertexai.api.GenerateContentResponse;importcom.google.cloud.vertexai.generativeai.GenerativeModel;importcom.google.cloud.vertexai.generativeai.ResponseHandler;importcom.google.cloud.ServiceOptions;@SpringBootApplicationpublicclassDemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}}@RestControllerclassHelloController{StringprojectId=ServiceOptions.getDefaultProjectId();@GetMapping("/")publicStringgetFacts(@RequestParam(defaultValue="dog")Stringanimal)throwsIOException{Stringprompt="Give me 10 fun facts about "+animal+". Return this as html without backticks.";VertexAIvertexAI=newVertexAI(projectId,null);GenerativeModelmodel=newGenerativeModel("gemini-1.5-flash",vertexAI);GenerateContentResponseresponse=model.generateContent(prompt);returnResponseHandler.getText(response);}}
12. 重新部署
請確認您仍在 Cloud Shell 的正確目錄中:
cd~/codelab-genai
執行下列指令,將應用程式的新版本提交至本機 Git 存放區:
gitadd.gitcommit-m"add latest changes"
將變更推送至 GitHub:
gitpush
建構作業完成後,請執行下列指令並前往已部署的應用程式:
echo-e"\n\nOnce the build finishes, visit your live application:\n \ "$(\gcloudrunserviceslist|\grepcodelab-genai|\awk'/URL/{print$2}'|\head-1\)" \n\n"
packagecom.example.demo;importjava.io.IOException;importjava.util.HashMap;importjava.util.logging.Level;importjava.util.logging.Logger;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importcom.google.cloud.vertexai.VertexAI;importcom.google.cloud.vertexai.api.GenerateContentResponse;importcom.google.cloud.vertexai.generativeai.GenerativeModel;importcom.google.cloud.vertexai.generativeai.ResponseHandler;importcom.google.cloud.ServiceOptions;@SpringBootApplicationpublicclassDemoApplication{publicstaticvoidmain(String[]args){SpringApplication.run(DemoApplication.class,args);}}@RestControllerclassHelloController{finalLoggerlogger=Logger.getLogger(HelloController.class.getName());StringprojectId=ServiceOptions.getDefaultProjectId();@GetMapping("/")publicStringgetFacts(@RequestParam(defaultValue="dog")Stringanimal)throwsIOException{Stringprompt="Give me 10 fun facts about "+animal+". Return this as html without backticks.";VertexAIvertexAI=newVertexAI(projectId,null);GenerativeModelmodel=newGenerativeModel("gemini-1.5-flash",vertexAI);GenerateContentResponseresponse=model.generateContent(prompt);logger.log(Level.INFO,"Content is generated",newHashMap<String,Object>(){{put("prompt",prompt);put("response",response);}});returnResponseHandler.getText(response);}}
這段程式碼會使用結構化記錄格式,將產生內容的資訊寫入 stdout。Cloud Run 中的記錄代理程式會擷取輸出內容,並將此格式寫入 Cloud Logging。stdout