
如何快速實現REST API集成以優化業務流程
然后根據Gemini API: Quickstart with Python ?|? Google AI for Developers根據教程來進行代碼編寫。
目前用這個API KEY可以使用Gemin pro ,Gemini vision。嘗試本地部署,但是會出現連接錯誤
所以必須采取曲線救國的方式,需要將demo運行在google認證的域里面,比如google colab,沒有的小伙伴可以自行申請一個。
然后我們在google colab上新建一個筆記,然后安裝好相應的依賴
! pip3 install --upgrade --user google-cloud-aiplatform!pip install google-generativeai==0.3.1!pip?install?python-dotenv
然后做好認證
import sys
# Additional authentication is required for Google Colabif "google.colab" in sys.modules: # Authenticate user to Google Cloud from google.colab import auth
auth.authenticate_user()
配置好API key
import pathlibimport textwrap
import google.generativeai as genai
genai.configure(api_key='AIzaSyBhLrxD8xAe154dqLXTI_xfkdY2dKa_KKw')
接下來就是簡單的chat配置
import google.generativeai as genaifrom dotenv import load_dotenvimport osfrom datetime import datetimeimport re
def main(): load_dotenv() #api_key = os.getenv("GEMINI_API_KEY")????api_key?=?'your api key' if not api_key: raise ValueError( "API key not found. Please set your GEMINI_API_KEY in the environment.")
genai.configure(api_key=api_key)
generation_config = { "temperature": 0.7, "top_p": 1, "top_k": 1, "max_output_tokens": 2048, }
safety_settings = { "HARM_CATEGORY_HARASSMENT": "BLOCK_NONE", "HARM_CATEGORY_HATE_SPEECH": "BLOCK_NONE", "HARM_CATEGORY_SEXUALLY_EXPLICIT": "BLOCK_NONE", "HARM_CATEGORY_DANGEROUS_CONTENT": "BLOCK_NONE", }
model = genai.GenerativeModel( 'gemini-pro', generation_config=generation_config, safety_settings=safety_settings) chat = model.start_chat(history=[])
while True: user_input = input("User: ").strip() try: response = chat.send_message(user_input, stream=True) response_text = "" for chunk in response: print(chunk.text) print("="*80)
except Exception as e: print(f"An error occurred: {e}")if __name__ == "__main__": main()
,整體來說和ChatGPT有的一拼,接下來我們使用Gemini vision,看看Gemini對圖片的理解能力如何
import google.generativeai as genaiimport osfrom dotenv import load_dotenvimport PIL.Image
img_vision = PIL.Image.open('/content/d040f28e.jpg')
def setup(): try:
# Load environment variables gemini_api_key = 'your api key' print(f"Gemini API Key: {gemini_api_key}") if gemini_api_key is None: raise Exception("GEMINI_API_KEY is not set") genai.configure(api_key=gemini_api_key) except Exception as exception: raise Exception("An error occurred during setup:", exception)
def generate_text(img): try: model = genai.GenerativeModel(model_name='gemini-pro-vision') response = model.generate_content(['寫一個故事關于這圖片',img]) return response.text except Exception as exception: raise Exception("An error occurred during text generation:", exception)
if __name__ == "__main__": try: setup() #print(f"Gemini Pro\nEnter a prompt:") #prompt = input() response = generate_text(img_vision) print(response) except Exception as exception: print(f"An error occurred: {exception}")
我們讓他根據這張圖片寫一個故事
結果如下:
整體的表述很清晰,時間、地點、人物、事件都描述的很清晰。
如果大家在體驗過程中出現400錯誤An error occurred: 400 Unknown error trying to retrieve streaming response. Please retry with stream=False
for more details.可以嘗試清除下瀏覽器緩存。
文章轉自微信公眾號@IoT Inn