|
1 |
| -Google Generative AI Python Client |
2 |
| -================================== |
| 1 | +# Google AI Python SDK |
3 | 2 |
|
4 | 3 | [](https://badge.fury.io/py/google-generativeai)
|
5 | 4 | 
|
6 | 5 | 
|
7 | 6 |
|
8 |
| -Get started using the PaLM API in Python. Check out the [developer site](https://developers.generativeai.google/) |
9 |
| -for comprehensive documentation. |
| 7 | +The Google AI Python SDK enables developers to use Google's state-of-the-art generative AI |
| 8 | +models (like Gemini and PaLM) to build AI-powered features and applications. This SDK |
| 9 | +supports use cases like: |
| 10 | + |
| 11 | +- Generate text from text-only input |
| 12 | +- Generate text from text-and-images input (multimodal) (for Gemini only) |
| 13 | +- Build multi-turn conversations (chat) |
| 14 | +- Embedding |
| 15 | + |
| 16 | +For example, with just a few lines of code, you can access Gemini's multimodal |
| 17 | +capabilities to generate text from text-and-image input: |
| 18 | + |
| 19 | +``` |
| 20 | +model = genai.GenerativeModel('gemini-pro-vision') |
| 21 | +
|
| 22 | +cookie_picture = [{ |
| 23 | + 'mime_type': 'image/png', |
| 24 | + 'data': Path('cookie.png').read_bytes() |
| 25 | +}] |
| 26 | +prompt = "Give me a recipe for this:" |
| 27 | +
|
| 28 | +response = model.generate_content( |
| 29 | + content=[prompt, cookie_picture] |
| 30 | +) |
| 31 | +print(response.text) |
| 32 | +``` |
10 | 33 |
|
11 |
| -## Installation and usage |
| 34 | + |
| 35 | +## Try out the API |
12 | 36 |
|
13 | 37 | Install from PyPI.
|
14 |
| -```bash |
15 |
| -pip install google-generativeai |
| 38 | + |
| 39 | +`pip install google-generativeai` |
| 40 | + |
| 41 | +[Obtain an API key from AI Studio](https://makersuite.google.com/app/apikey), |
| 42 | +then configure it here. |
| 43 | + |
| 44 | +Import the SDK and load a model. |
| 45 | + |
16 | 46 | ```
|
| 47 | +import google.generativeai as genai |
| 48 | +
|
| 49 | +genai.configure(api_key=os.environ["API_KEY"]) |
| 50 | +
|
| 51 | +model = genai.GenerativeModel('gemini-pro') |
| 52 | +``` |
| 53 | + |
| 54 | +Use `GenerativeModel.generate_content` to have the model complete some initial text. |
| 55 | + |
| 56 | +``` |
| 57 | +response = model.generate_content("The opposite of hot is") |
| 58 | +print(response.text) # cold. |
| 59 | +``` |
| 60 | + |
| 61 | +Use `GenerativeModel.start_chat` to have a discussion with a model. |
| 62 | + |
| 63 | +``` |
| 64 | +chat = model.start_chat() |
| 65 | +response = chat.send_message('Hello, what should I have for dinner?') |
| 66 | +print(response.text) # 'Here are some suggestions...' |
| 67 | +response = chat.send_message("How do I cook the first one?") |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +## Installation and usage |
| 73 | + |
| 74 | +Run [`pip install google-generativeai`](https://pypi.org/project/google-generativeai). |
17 | 75 |
|
18 |
| -Get an [API key from MakerSuite](https://makersuite.google.com/app/apikey), then configure it here. |
19 |
| -```python |
| 76 | +For detailed instructions, you can find a |
| 77 | +[quickstart](https://ai.google.dev/tutorials/python_quickstart) for the Google AI |
| 78 | +Python SDK in the Google documentation. |
| 79 | + |
| 80 | +This quickstart describes how to add your API key and install the SDK in your app, |
| 81 | +initialize the model, and then call the API to access the model. It also describes some |
| 82 | +additional use cases and features, like streaming, embedding, counting tokens, and |
| 83 | +controlling responses. |
| 84 | + |
| 85 | + |
| 86 | +## Documentation |
| 87 | + |
| 88 | +Find complete documentation for the Google AI SDKs and the Gemini model in the Google |
| 89 | +documentation: https://ai.google.dev/docs |
| 90 | + |
| 91 | + |
| 92 | +## Contributing |
| 93 | + |
| 94 | +See [Contributing](https://github.com/google/generative-ai-python/blob/main/CONTRIBUTING.md) for more information on contributing to the Google AI JavaScript SDK. |
| 95 | + |
| 96 | +## Developers who use the PaLM API |
| 97 | + |
| 98 | +### Migrate to use the Gemini API |
| 99 | + |
| 100 | +Check our [migration guide](https://ai.google.dev/docs/migration_guide) in the Google |
| 101 | +documentation. |
| 102 | + |
| 103 | +### Installation and usage for the PaLM API |
| 104 | + |
| 105 | +Install from PyPI. |
| 106 | + |
| 107 | +`pip install google-generativeai` |
| 108 | + |
| 109 | +[Obtain an API key from AI Studio](https://makersuite.google.com/app/apikey), then |
| 110 | +configure it here. |
| 111 | + |
| 112 | +``` |
20 | 113 | import google.generativeai as palm
|
21 | 114 |
|
22 | 115 | palm.configure(api_key=os.environ["PALM_API_KEY"])
|
23 | 116 | ```
|
24 | 117 |
|
25 |
| -Use [`palm.generate_text`](https://developers.generativeai.google/api/python/google/generativeai/generate_text) |
26 |
| -to have the model complete some initial text. |
27 |
| -```python |
| 118 | +Use `palm.generate_text` to have the model complete some initial text. |
| 119 | + |
| 120 | +``` |
28 | 121 | response = palm.generate_text(prompt="The opposite of hot is")
|
29 | 122 | print(response.result) # cold.
|
30 | 123 | ```
|
31 | 124 |
|
32 |
| -Use [`palm.chat`](https://developers.generativeai.google/api/python/google/generativeai/chat) |
33 |
| -to have a discussion with a model. |
34 |
| -```python |
| 125 | +Use `palm.chat` to have a discussion with a model. |
| 126 | + |
| 127 | +``` |
35 | 128 | response = palm.chat(messages=["Hello."])
|
36 | 129 | print(response.last) # 'Hello! What can I help you with?'
|
37 | 130 | response.reply("Can you tell me a joke?")
|
38 | 131 | ```
|
39 | 132 |
|
40 |
| -## Documentation |
| 133 | +### Documentation for the PaLM API |
| 134 | + |
| 135 | +- [General PaLM documentation](https://ai.google.dev/docs/palm_api_overview) |
| 136 | + |
| 137 | +- [Text quickstart](https://github.com/google/generative-ai-docs/tree/main/site/en/tutorials/text_quickstart.ipynb) |
| 138 | + |
| 139 | +- [Chat quickstart](https://github.com/google/generative-ai-docs/tree/main/site/en/tutorials/chat_quickstart.ipynb) |
41 | 140 |
|
42 |
| -Checkout the full [API docs](https://developers.generativeai.google/api), the [guide](https://developers.generativeai.google/guide) and [quick starts](https://developers.generativeai.google/tutorials). |
| 141 | +- [Tuning quickstart](https://github.com/google/generative-ai-docs/blob/main/site/en/tutorials/tuning_quickstart_python.ipynb) |
43 | 142 |
|
44 |
| -## Colab magics |
| 143 | +### Colab magics |
45 | 144 |
|
46 |
| -Once installed, use the Python client via the `%%palm` Colab magic. Read the full guide [here](https://developers.generativeai.google/tools/notebook_magic). |
| 145 | +Once installed, use the Python client via the `%%palm` Colab magic. Read the [full guide](https://github.com/google/generative-ai-docs/blob/main/site/en/tools/notebook_magic.ipynb). |
47 | 146 |
|
48 |
| -```python |
| 147 | +``` |
49 | 148 | %%palm
|
50 | 149 | The best thing since sliced bread is
|
51 | 150 | ```
|
| 151 | + |
| 152 | +## License |
| 153 | + |
| 154 | +The contents of this repository are licensed under the [Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0). |
0 commit comments