Integrating Large Language Model APIs
Now that you understand Python variables and operations, let's connect to the Google Gemini API to build a simple interactive chat assistant.
Setting Up Google Gemini SDK
Install the Google generative AI library:
bash lang-bash
1
pip install google-generativeaiWriting the Code
Make sure you set your API key as an environment variable:
export GEMINI_API_KEY="your-api-key" (or set on Windows).
python lang-python
1
2
3
4
5
6
7
8
9
10
11
12
13
import osimport google.generativeai as genai # Load API key from envapi_key = os.environ.get("GEMINI_API_KEY")genai.configure(api_key=api_key) # Initialize modelmodel = genai.GenerativeModel('gemini-1.5-flash') # Prompt modelresponse = model.generate_content("Give me a 3-word slogan for CourseDock.")print(response.text)