Anleitung: Erste Schritte mit der Gemini API


Bei Google AI ansehen In Google Colab ausführen Quelle auf GitHub ansehen

In dieser Kurzanleitung wird gezeigt, wie Sie das Python SDK für die Gemini API verwenden. erhältst du Zugriff auf die Large Language Models von Gemini von Google. In dieser Kurzanleitung lernen Sie Folgendes:

  1. Richten Sie Ihre Entwicklungsumgebung und den API-Zugriff ein, um Gemini zu verwenden.
  2. Textantworten aus Texteingaben generieren.
  3. Textantworten aus multimodalen Eingaben (Text und Bilder) generieren.
  4. Verwenden Sie Gemini für Unterhaltungen in mehreren Schritten (Chat).
  5. Verwenden Sie Einbettungen für Large Language Models.

Vorbereitung

Sie können diesen Schnellstart in Google Colab das dieses Notebook direkt im Browser ausführt und keine zusätzlichen Konfiguration der Umgebung.

Wenn Sie diese Kurzanleitung lokal ausführen möchten, müssen Sie sicherstellen, dass Ihre Entwicklung die folgenden Anforderungen erfüllt:

  • Python 3.9 oder höher
  • Eine Installation von jupyter zum Ausführen des Notebooks.

Einrichtung

Python SDK installieren

Das Python SDK für die Gemini API ist im google-generativeai-Paket. Installieren Sie die Abhängigkeit mithilfe von pip:

pip install -q -U google-generativeai

Pakete importieren

Importieren Sie die erforderlichen Pakete.

import pathlib
import textwrap

import google.generativeai as genai

from IPython.display import display
from IPython.display import Markdown

def to_markdown(text):
  text = text.replace('•', '  *')
  return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
# Used to securely store your API key
from google.colab import userdata

API-Schlüssel einrichten

Bevor Sie die Gemini API verwenden können, müssen Sie einen API-Schlüssel abrufen. Wenn Sie noch keinen haben, erstellen Sie mit einem Klick einen Schlüssel in Google AI Studio.

API-Schlüssel anfordern

Fügen Sie den Schlüssel in Colab dem Secret-Manager unter dem Menüpunkt „🚀“ hinzu. im linken Bereich. Geben Sie ihr den Namen GOOGLE_API_KEY.

Sobald Sie den API-Schlüssel haben, übergeben Sie ihn an das SDK. Dafür haben Sie die beiden folgenden Möglichkeiten:

  • Fügen Sie den Schlüssel in die Umgebungsvariable GOOGLE_API_KEY ein. Das SDK wird und holt sie dort automatisch ein.)
  • Übergib den Schlüssel an genai.configure(api_key=...)
# Or use `os.getenv('GOOGLE_API_KEY')` to fetch an environment variable.
GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')

genai.configure(api_key=GOOGLE_API_KEY)

Modelle auflisten

Jetzt können Sie die Gemini API aufrufen. Verwende list_models, um die verfügbaren Gemini-Modelle:

  • gemini-1.5-flash: unser schnellstes multimodales Modell
  • gemini-1.5-pro: unser leistungsstärkstes und intelligentes multimodales Modell
for m in genai.list_models():
  if 'generateContent' in m.supported_generation_methods:
    print(m.name)

Text aus Texteingaben generieren

Verwenden Sie für reine Text-Prompts ein Gemini 1.5-Modell oder Gemini 1.0 Pro:

model = genai.GenerativeModel('gemini-1.5-flash')

Die Methode generate_content eignet sich für eine Vielzahl von Anwendungsfällen, darunter: mehrstufiger Chat und multimodale Eingabe, je nach zugrunde liegendem Modell unterstützt. Die verfügbaren Modelle unterstützen nur Text und Bilder als Eingabe sowie Text als Ausgabe.

Im einfachsten Fall können Sie einen Eingabeaufforderungsstring an den GenerativeModel.generate_content :

%%time
response = model.generate_content("What is the meaning of life?")
CPU times: user 110 ms, sys: 12.3 ms, total: 123 ms
Wall time: 8.25 s

In einfachen Fällen benötigen Sie lediglich die Zugriffsfunktion response.text. Zum Anzeigen formatierten Markdown-Text verwenden, verwenden Sie die Funktion to_markdown:

to_markdown(response.text)
The query of life's purpose has perplexed people across centuries, cultures, and continents. While there is no universally recognized response, many ideas have been put forth, and the response is frequently dependent on individual ideas, beliefs, and life experiences.

1.  **Happiness and Well-being:** Many individuals believe that the goal of life is to attain personal happiness and well-being. This might entail locating pursuits that provide joy, establishing significant connections, caring for one's physical and mental health, and pursuing personal goals and interests.

2.  **Meaningful Contribution:** Some believe that the purpose of life is to make a meaningful contribution to the world. This might entail pursuing a profession that benefits others, engaging in volunteer or charitable activities, generating art or literature, or inventing.

3.  **Self-realization and Personal Growth:** The pursuit of self-realization and personal development is another common goal in life. This might entail learning new skills, pushing one's boundaries, confronting personal obstacles, and evolving as a person.

4.  **Ethical and Moral Behavior:** Some believe that the goal of life is to act ethically and morally. This might entail adhering to one's moral principles, doing the right thing even when it is difficult, and attempting to make the world a better place.

5.  **Spiritual Fulfillment:** For some, the purpose of life is connected to spiritual or religious beliefs. This might entail seeking a connection with a higher power, practicing religious rituals, or following spiritual teachings.

6.  **Experiencing Life to the Fullest:** Some individuals believe that the goal of life is to experience all that it has to offer. This might entail traveling, trying new things, taking risks, and embracing new encounters.

7.  **Legacy and Impact:** Others believe that the purpose of life is to leave a lasting legacy and impact on the world. This might entail accomplishing something noteworthy, being remembered for one's contributions, or inspiring and motivating others.

8.  **Finding Balance and Harmony:** For some, the purpose of life is to find balance and harmony in all aspects of their lives. This might entail juggling personal, professional, and social obligations, seeking inner peace and contentment, and living a life that is in accordance with one's values and beliefs.

Ultimately, the meaning of life is a personal journey, and different individuals may discover their own unique purpose through their experiences, reflections, and interactions with the world around them.

Wenn die API kein Ergebnis zurückgeben konnte, verwenden Sie GenerateContentResponse.prompt_feedback um zu sehen, ob sie aufgrund von Sicherheitsbedenken hinsichtlich des Prompts blockiert wurde.

response.prompt_feedback
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}

Gemini kann mehrere mögliche Antworten für einen einzelnen Prompt generieren. Diese Mögliche Antworten heißen candidates. Sie können sie sich ansehen, die am besten geeignete Antwort auswählen.

Antwortvorschläge anzeigen mit GenerateContentResponse.candidates:

response.candidates
[
  content {
    parts {
      text: "The query of life\'s purpose has perplexed people across centuries, cultures, and continents. While there is no universally recognized response, many ideas have been put forth, and the response is frequently dependent on individual ideas, beliefs, and life experiences.\n\n1. **Happiness and Well-being:** Many individuals believe that the goal of life is to attain personal happiness and well-being. This might entail locating pursuits that provide joy, establishing significant connections, caring for one\'s physical and mental health, and pursuing personal goals and interests.\n\n2. **Meaningful Contribution:** Some believe that the purpose of life is to make a meaningful contribution to the world. This might entail pursuing a profession that benefits others, engaging in volunteer or charitable activities, generating art or literature, or inventing.\n\n3. **Self-realization and Personal Growth:** The pursuit of self-realization and personal development is another common goal in life. This might entail learning new skills, pushing one\'s boundaries, confronting personal obstacles, and evolving as a person.\n\n4. **Ethical and Moral Behavior:** Some believe that the goal of life is to act ethically and morally. This might entail adhering to one\'s moral principles, doing the right thing even when it is difficult, and attempting to make the world a better place.\n\n5. **Spiritual Fulfillment:** For some, the purpose of life is connected to spiritual or religious beliefs. This might entail seeking a connection with a higher power, practicing religious rituals, or following spiritual teachings.\n\n6. **Experiencing Life to the Fullest:** Some individuals believe that the goal of life is to experience all that it has to offer. This might entail traveling, trying new things, taking risks, and embracing new encounters.\n\n7. **Legacy and Impact:** Others believe that the purpose of life is to leave a lasting legacy and impact on the world. This might entail accomplishing something noteworthy, being remembered for one\'s contributions, or inspiring and motivating others.\n\n8. **Finding Balance and Harmony:** For some, the purpose of life is to find balance and harmony in all aspects of their lives. This might entail juggling personal, professional, and social obligations, seeking inner peace and contentment, and living a life that is in accordance with one\'s values and beliefs.\n\nUltimately, the meaning of life is a personal journey, and different individuals may discover their own unique purpose through their experiences, reflections, and interactions with the world around them."
    }
    role: "model"
  }
  finish_reason: STOP
  index: 0
  safety_ratings {
    category: HARM_CATEGORY_SEXUALLY_EXPLICIT
    probability: NEGLIGIBLE
  }
  safety_ratings {
    category: HARM_CATEGORY_HATE_SPEECH
    probability: NEGLIGIBLE
  }
  safety_ratings {
    category: HARM_CATEGORY_HARASSMENT
    probability: NEGLIGIBLE
  }
  safety_ratings {
    category: HARM_CATEGORY_DANGEROUS_CONTENT
    probability: NEGLIGIBLE
  }
]

Standardmäßig gibt das Modell nach Abschluss der gesamten Generierung eine Antwort zurück . Sie können die Antwort auch streamen, während sie generiert wird, und der gibt das Modell Teile der Antwort zurück, sobald diese generiert wurden.

Verwenden Sie GenerativeModel.generate_content(..., stream=True), um Antworten zu streamen.

%%time
response = model.generate_content("What is the meaning of life?", stream=True)
CPU times: user 102 ms, sys: 25.1 ms, total: 128 ms
Wall time: 7.94 s
for chunk in response:
  print(chunk.text)
  print("_"*80)
The query of life's purpose has perplexed people across centuries, cultures, and
________________________________________________________________________________
 continents. While there is no universally recognized response, many ideas have been put forth, and the response is frequently dependent on individual ideas, beliefs, and life experiences
________________________________________________________________________________
.

1.  **Happiness and Well-being:** Many individuals believe that the goal of life is to attain personal happiness and well-being. This might entail locating pursuits that provide joy, establishing significant connections, caring for one's physical and mental health, and pursuing personal goals and aspirations.

2.  **Meaning
________________________________________________________________________________
ful Contribution:** Some believe that the purpose of life is to make a meaningful contribution to the world. This might entail pursuing a profession that benefits others, engaging in volunteer or charitable activities, generating art or literature, or inventing.

3.  **Self-realization and Personal Growth:** The pursuit of self-realization and personal development is another common goal in life. This might entail learning new skills, exploring one's interests and abilities, overcoming obstacles, and becoming the best version of oneself.

4.  **Connection and Relationships:** For many individuals, the purpose of life is found in their relationships with others. This might entail building
________________________________________________________________________________
 strong bonds with family and friends, fostering a sense of community, and contributing to the well-being of those around them.

5.  **Spiritual Fulfillment:** For those with religious or spiritual beliefs, the purpose of life may be centered on seeking spiritual fulfillment or enlightenment. This might entail following religious teachings, engaging in spiritual practices, or seeking a deeper understanding of the divine.

6.  **Experiencing the Journey:** Some believe that the purpose of life is simply to experience the journey itself, with all its joys and sorrows. This perspective emphasizes embracing the present moment, appreciating life's experiences, and finding meaning in the act of living itself.

7.  **Legacy and Impact:** For others, the goal of life is to leave a lasting legacy or impact on the world. This might entail making a significant contribution to a particular field, leaving a positive mark on future generations, or creating something that will be remembered and cherished long after one's lifetime.

Ultimately, the meaning of life is a personal and subjective question, and there is no single, universally accepted answer. It is about discovering what brings you fulfillment, purpose, and meaning in your own life, and living in accordance with those values.
________________________________________________________________________________

Beim Streaming sind einige Antwortattribute erst verfügbar, wenn Sie iteriert haben durch alle Antwortblöcke. Dies wird im Folgenden dargestellt:

response = model.generate_content("What is the meaning of life?", stream=True)

Das Attribut prompt_feedback funktioniert so:

response.prompt_feedback
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}

Bei Attributen wie text ist das jedoch nicht der Fall:

try:
  response.text
except Exception as e:
  print(f'{type(e).__name__}: {e}')
IncompleteIterationError: Please let the response complete iteration before accessing the final accumulated
attributes (or call `response.resolve()`)

Text aus Bild- und Texteingaben generieren

Gemini bietet verschiedene Modelle, die multimodale Eingaben verarbeiten können (Gemini 1.5). Modelle), sodass Sie sowohl Text als auch Bilder eingeben können. Sehen Sie sich die Bildanforderungen für Prompts

Wenn die Prompt-Eingabe sowohl Text als auch Bilder enthält, verwenden Sie Gemini 1.5 mit dem Methode GenerativeModel.generate_content zum Generieren der Textausgabe:

Fügen wir ein Bild ein:

curl -o image.jpg https://t0.gstatic.com/licensed-image?q=tbn:ANd9GcQ_Kevbk21QBRy-PgB4kQpS79brbmmEG7m3VOTShAn4PecDU5H5UxrJxE3Dw1JiaG17V88QIol19-3TM2wCHw
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  405k  100  405k    0     0  6982k      0 --:--:-- --:--:-- --:--:-- 7106k
import PIL.Image

img = PIL.Image.open('image.jpg')
img

png

Verwenden Sie ein Gemini 1.5-Modell und übergeben Sie das Bild mit generate_content an das Modell.

model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(img)

to_markdown(response.text)
Chicken Teriyaki Meal Prep Bowls with brown rice, roasted broccoli and bell peppers.

Wenn Sie in einem Prompt sowohl Text als auch Bilder angeben möchten, übergeben Sie eine Liste mit den Strings und Bilder:

response = model.generate_content(["Write a short, engaging blog post based on this picture. It should include a description of the meal in the photo and talk about my journey meal prepping.", img], stream=True)
response.resolve()
to_markdown(response.text)
Meal prepping is a great way to save time and money, and it can also help you to eat healthier. This meal is a great example of a healthy and delicious meal that can be easily prepped ahead of time.

This meal features brown rice, roasted vegetables, and chicken teriyaki. The brown rice is a whole grain that is high in fiber and nutrients. The roasted vegetables are a great way to get your daily dose of vitamins and minerals. And the chicken teriyaki is a lean protein source that is also packed with flavor.

This meal is easy to prepare ahead of time. Simply cook the brown rice, roast the vegetables, and cook the chicken teriyaki. Then, divide the meal into individual containers and store them in the refrigerator. When you're ready to eat, simply grab a container and heat it up.

This meal is a great option for busy people who are looking for a healthy and delicious way to eat. It's also a great meal for those who are trying to lose weight or maintain a healthy weight.

If you're looking for a healthy and delicious meal that can be easily prepped ahead of time, this meal is a great option. Give it a try today!

Chatunterhaltungen

Mit Gemini kannst du dich über mehrere Runden hinweg unterhalten. Die Die Klasse ChatSession vereinfacht den Prozess, da sie den Status des Konversation. Im Gegensatz zu generate_content müssen Sie das Element also nicht Konversationsverlauf als Liste anzeigen.

Initialisieren Sie den Chat:

model = genai.GenerativeModel('gemini-1.5-flash')
chat = model.start_chat(history=[])
chat
<google.generativeai.generative_models.ChatSession at 0x7b7b68250100>

Die ChatSession.send_message gibt denselben GenerateContentResponse-Typ zurück wie GenerativeModel.generate_content Außerdem werden Ihre Nachricht und die Antwort an das Chatprotokoll angehängt:

response = chat.send_message("In one sentence, explain how a computer works to a young child.")
to_markdown(response.text)
A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!
chat.history
[
  parts {
    text: "In one sentence, explain how a computer works to a young child."
  }
  role: "user",
  parts {
    text: "A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!"
  }
  role: "model"
]

Du kannst weiterhin Nachrichten senden, um die Unterhaltung fortzusetzen. Verwenden Sie die Methode Argument stream=True, um den Chat zu streamen:

response = chat.send_message("Okay, how about a more detailed explanation to a high schooler?", stream=True)

for chunk in response:
  print(chunk.text)
  print("_"*80)
A computer works by following instructions, called a program, which tells it what to
________________________________________________________________________________
 do. These instructions are written in a special language that the computer can understand, and they are stored in the computer's memory. The computer's processor
________________________________________________________________________________
, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program's logic. The results of these calculations and decisions are then displayed on the computer's screen or stored in memory for later use.

To give you a simple analogy, imagine a computer as a
________________________________________________________________________________
 chef following a recipe. The recipe is like the program, and the chef's actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).

In summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results.
________________________________________________________________________________

glm.Content-Objekte enthalten eine Liste von glm.Part-Objekten, die jeweils Folgendes enthalten: entweder einen Text (String) oder inline_data (glm.Blob), wobei ein Blob Binärdaten enthält. Daten und mime_type. Das Chatprotokoll ist als Liste mit glm.Content verfügbar. Objekte in ChatSession.history:

for message in chat.history:
  display(to_markdown(f'**{message.role}**: {message.parts[0].text}'))
**user**: In one sentence, explain how a computer works to a young child.

**model**: A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!

**user**: Okay, how about a more detailed explanation to a high schooler?

**model**: A computer works by following instructions, called a program, which tells it what to do. These instructions are written in a special language that the computer can understand, and they are stored in the computer's memory. The computer's processor, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program's logic. The results of these calculations and decisions are then displayed on the computer's screen or stored in memory for later use.

To give you a simple analogy, imagine a computer as a chef following a recipe. The recipe is like the program, and the chef's actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).

In summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results.

Anzahl der Tokens

Large Language Models haben ein Kontextfenster und die Kontextlänge ist oft gemessen an der Anzahl der Tokens. Mit der Gemini API können Sie Folgendes tun: Bestimmt die Anzahl der Tokens pro genai.protos.Content-Objekt. Im Im einfachsten Fall können Sie einen Abfragestring an den GenerativeModel.count_tokens so an:

model.count_tokens("What is the meaning of life?")
total_tokens: 7

Ebenso können Sie token_count auf ChatSession prüfen:

model.count_tokens(chat.history)
total_tokens: 501

Einbettungen verwenden

Einbetten ist eine Technik, die verwendet wird, um Informationen als Liste von Gleitkommazahlen darzustellen in einem Array. Mit Gemini können Sie Text darstellen (Wörter, Sätze und Blöcke, Text) in einer vektorisierten Form, was den Vergleich und die Gegenüberstellung Einbettungen. Beispiel: Zwei Texte zu einem ähnlichen Thema Die Stimmung sollte ähnliche Einbettungen haben, die durch mathematische Vergleichstechniken wie die Kosinus-Ähnlichkeit. Weitere Informationen dazu, und warum Sie Einbettungen verwenden sollten, finden Sie unter Einbettungen .

Verwenden Sie die Methode embed_content, um Einbettungen zu generieren. Die Methode verarbeitet Einbettung für die folgenden Aufgaben (task_type):

Aufgabentyp Beschreibung
RETRIEVAL_QUERY Gibt an, dass der angegebene Text eine Abfrage in einer Such-/Abrufeinstellung ist.
RETRIEVAL_DOCUMENT Gibt an, dass der angegebene Text ein Dokument in einer Such-/Abrufeinstellung ist. Für diesen Aufgabentyp ist ein title erforderlich.
SEMANTIC_SIMILARITY Gibt an, dass der angegebene Text für die Bestimmung der semantischen Textähnlichkeit (Semantic Textual Similarity, STS) verwendet wird.
KLASSIFIZIERUNG Gibt an, dass die Einbettungen zur Klassifizierung verwendet werden.
Clustering Gibt an, dass die Einbettungen für das Clustering verwendet werden.

Mit dem folgenden Befehl wird eine Einbettung für einen einzelnen String zum Abrufen des Dokuments generiert:

result = genai.embed_content(
    model="models/embedding-001",
    content="What is the meaning of life?",
    task_type="retrieval_document",
    title="Embedding of single string")

# 1 input > 1 vector output
print(str(result['embedding'])[:50], '... TRIMMED]')
[-0.003216741, -0.013358698, -0.017649598, -0.0091 ... TRIMMED]

Übergeben Sie in content eine Liste von Strings, um String-Batches zu verarbeiten:

result = genai.embed_content(
    model="models/embedding-001",
    content=[
      'What is the meaning of life?',
      'How much wood would a woodchuck chuck?',
      'How does the brain work?'],
    task_type="retrieval_document",
    title="Embedding of list of strings")

# A list of inputs > A list of vectors output
for v in result['embedding']:
  print(str(v)[:50], '... TRIMMED ...')
[0.0040260437, 0.004124458, -0.014209415, -0.00183 ... TRIMMED ...
[-0.004049845, -0.0075574904, -0.0073463684, -0.03 ... TRIMMED ...
[0.025310587, -0.0080734305, -0.029902633, 0.01160 ... TRIMMED ...

Die genai.embed_content-Funktion akzeptiert zwar Strings oder Stringlisten, auf dem genai.protos.Content-Typ basiert (wie GenerativeModel.generate_content) glm.Content-Objekte sind die primären Konversationseinheiten in der API.

Während das genai.protos.Content-Objekt multimodal ist, ist das embed_content unterstützt nur Texteinbettungen. Durch dieses Design erhält die API Möglichkeit der Erweiterung auf multimodale Einbettungen.

response.candidates[0].content
parts {
  text: "A computer works by following instructions, called a program, which tells it what to do. These instructions are written in a special language that the computer can understand, and they are stored in the computer\'s memory. The computer\'s processor, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program\'s logic. The results of these calculations and decisions are then displayed on the computer\'s screen or stored in memory for later use.\n\nTo give you a simple analogy, imagine a computer as a chef following a recipe. The recipe is like the program, and the chef\'s actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).\n\nIn summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results."
}
role: "model"
result = genai.embed_content(
    model = 'models/embedding-001',
    content = response.candidates[0].content)

# 1 input > 1 vector output
print(str(result['embedding'])[:50], '... TRIMMED ...')
[-0.013921871, -0.03504407, -0.0051786783, 0.03113 ... TRIMMED ...

Ebenso enthält das Chatprotokoll eine Liste von genai.protos.Content-Objekten, das Sie direkt an die Funktion embed_content übergeben können:

chat.history
[
  parts {
    text: "In one sentence, explain how a computer works to a young child."
  }
  role: "user",
  parts {
    text: "A computer is like a very smart machine that can understand and follow our instructions, help us with our work, and even play games with us!"
  }
  role: "model",
  parts {
    text: "Okay, how about a more detailed explanation to a high schooler?"
  }
  role: "user",
  parts {
    text: "A computer works by following instructions, called a program, which tells it what to do. These instructions are written in a special language that the computer can understand, and they are stored in the computer\'s memory. The computer\'s processor, or CPU, reads the instructions from memory and carries them out, performing calculations and making decisions based on the program\'s logic. The results of these calculations and decisions are then displayed on the computer\'s screen or stored in memory for later use.\n\nTo give you a simple analogy, imagine a computer as a chef following a recipe. The recipe is like the program, and the chef\'s actions are like the instructions the computer follows. The chef reads the recipe (the program) and performs actions like gathering ingredients (fetching data from memory), mixing them together (performing calculations), and cooking them (processing data). The final dish (the output) is then presented on a plate (the computer screen).\n\nIn summary, a computer works by executing a series of instructions, stored in its memory, to perform calculations, make decisions, and display or store the results."
  }
  role: "model"
]
result = genai.embed_content(
    model = 'models/embedding-001',
    content = chat.history)

# 1 input > 1 vector output
for i,v in enumerate(result['embedding']):
  print(str(v)[:50], '... TRIMMED...')
[-0.014632266, -0.042202696, -0.015757175, 0.01548 ... TRIMMED...
[-0.010979066, -0.024494737, 0.0092659835, 0.00803 ... TRIMMED...
[-0.010055617, -0.07208932, -0.00011750793, -0.023 ... TRIMMED...
[-0.013921871, -0.03504407, -0.0051786783, 0.03113 ... TRIMMED...

Komplexere Anwendungsfälle

In den folgenden Abschnitten werden erweiterte Anwendungsfälle und Low-Level-Details Python SDK für die Gemini API

Sicherheits­einstellungen

Mit dem Argument safety_settings können Sie konfigurieren, was das Modell blockiert und sowohl Prompts als auch Antworten zulassen. Inhalte werden standardmäßig durch Sicherheitseinstellungen blockiert mit mittlerer und/oder hoher Wahrscheinlichkeit für unsichere Inhalte in allen Dimensionen. Weitere Informationen zur Sicherheit Einstellungen.

Geben Sie einen fragwürdigen Prompt ein und führen Sie das Modell mit den standardmäßigen Sicherheitseinstellungen aus. und es werden keine Kandidaten zurückgegeben:

response = model.generate_content('[Questionable prompt here]')
response.candidates
[
  content {
    parts {
      text: "I\'m sorry, but this prompt involves a sensitive topic and I\'m not allowed to generate responses that are potentially harmful or inappropriate."
    }
    role: "model"
  }
  finish_reason: STOP
  index: 0
  safety_ratings {
    category: HARM_CATEGORY_SEXUALLY_EXPLICIT
    probability: NEGLIGIBLE
  }
  safety_ratings {
    category: HARM_CATEGORY_HATE_SPEECH
    probability: NEGLIGIBLE
  }
  safety_ratings {
    category: HARM_CATEGORY_HARASSMENT
    probability: NEGLIGIBLE
  }
  safety_ratings {
    category: HARM_CATEGORY_DANGEROUS_CONTENT
    probability: NEGLIGIBLE
  }
]

prompt_feedback teilt dir mit, welcher Sicherheitsfilter den Prompt blockiert hat:

response.prompt_feedback
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}

Geben Sie nun den gleichen Prompt mit den neu konfigurierten Sicherheitseinstellungen an das Modell an. und Sie erhalten möglicherweise eine Antwort.

response = model.generate_content('[Questionable prompt here]',
                                  safety_settings={'HARASSMENT':'block_none'})
response.text

Beachten Sie außerdem, dass jeder Kandidat eine eigene safety_ratings hat, falls die Aufforderung die Prüfung besteht, aber die einzelnen Antworten bestehen die Sicherheitschecks nicht.

Nachrichten codieren

In den vorherigen Abschnitten wurde das SDK verwendet, um Ihnen das Senden von Prompts zu erleichtern. an die API senden. Dieser Abschnitt enthält ein vollständig typisiertes Äquivalent zum vorherigen damit Sie die untergeordneten Details in Bezug auf die Funktionsweise der Das SDK codiert Nachrichten.

Das SDK versucht, die Nachricht in ein genai.protos.Content-Objekt zu konvertieren. mit einer Liste von genai.protos.Part-Objekten, die jeweils eines der folgenden Elemente enthalten:

  1. ein text (String)
  2. inline_data (genai.protos.Blob), wobei ein Blob den Binärwert data und mime_type.
  3. oder andere Datentypen.

Sie können jede dieser Klassen auch als äquivalentes Wörterbuch übergeben.

Das vollständig typisierte Äquivalent zum vorherigen Beispiel ist also:

model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(
    genai.protos.Content(
        parts = [
            genai.protos.Part(text="Write a short, engaging blog post based on this picture."),
            genai.protos.Part(
                inline_data=genai.protos.Blob(
                    mime_type='image/jpeg',
                    data=pathlib.Path('image.jpg').read_bytes()
                )
            ),
        ],
    ),
    stream=True)
response.resolve()

to_markdown(response.text[:100] + "... [TRIMMED] ...")
Meal prepping is a great way to save time and money, and it can also help you to eat healthier. By ... [TRIMMED] ...

Unterhaltungen über mehrere Themen

Die zuvor gezeigte Klasse genai.ChatSession eignet sich für viele Anwendungsfälle, trifft jedoch einige Annahmen. Wenn Ihr Anwendungsfall in diesen Chat nicht passt dass genai.ChatSession nur ein Wrapper ist, um GenerativeModel.generate_content Neben Einzelanfragen können auch Unterhaltungen mit mehreren Themen verarbeitet werden.

Die einzelnen Nachrichten sind genai.protos.Content-Objekte oder kompatible Wörterbücher, wie in den vorherigen Abschnitten beschrieben. Als Wörterbuch enthält die Nachricht erfordert die Schlüssel role und parts. Das role in einer Unterhaltung kann entweder der user für die Prompts oder model für die Antworten.

Übergeben Sie eine Liste mit genai.protos.Content-Objekten. Diese wird behandelt als Mehrstufiger Chat:

model = genai.GenerativeModel('gemini-1.5-flash')

messages = [
    {'role':'user',
     'parts': ["Briefly explain how a computer works to a young child."]}
]
response = model.generate_content(messages)

to_markdown(response.text)
Imagine a computer as a really smart friend who can help you with many things. Just like you have a brain to think and learn, a computer has a brain too, called a processor. It's like the boss of the computer, telling it what to do.

Inside the computer, there's a special place called memory, which is like a big storage box. It remembers all the things you tell it to do, like opening games or playing videos.

When you press buttons on the keyboard or click things on the screen with the mouse, you're sending messages to the computer. These messages travel through special wires, called cables, to the processor.

The processor reads the messages and tells the computer what to do. It can open programs, show you pictures, or even play music for you.

All the things you see on the screen are created by the graphics card, which is like a magic artist inside the computer. It takes the processor's instructions and turns them into colorful pictures and videos.

To save your favorite games, videos, or pictures, the computer uses a special storage space called a hard drive. It's like a giant library where the computer can keep all your precious things safe.

And when you want to connect to the internet to play games with friends or watch funny videos, the computer uses something called a network card to send and receive messages through the internet cables or Wi-Fi signals.

So, just like your brain helps you learn and play, the computer's processor, memory, graphics card, hard drive, and network card all work together to make your computer a super-smart friend that can help you do amazing things!

Fügen Sie die Antwort und eine weitere Nachricht hinzu, um die Unterhaltung fortzusetzen.

messages.append({'role':'model',
                 'parts':[response.text]})

messages.append({'role':'user',
                 'parts':["Okay, how about a more detailed explanation to a high school student?"]})

response = model.generate_content(messages)

to_markdown(response.text)
At its core, a computer is a machine that can be programmed to carry out a set of instructions. It consists of several essential components that work together to process, store, and display information:

**1. Processor (CPU):**
   -   The brain of the computer.
   -   Executes instructions and performs calculations.
   -   Speed measured in gigahertz (GHz).
   -   More GHz generally means faster processing.

**2. Memory (RAM):**
   -   Temporary storage for data being processed.
   -   Holds instructions and data while the program is running.
   -   Measured in gigabytes (GB).
   -   More GB of RAM allows for more programs to run simultaneously.

**3. Storage (HDD/SSD):**
   -   Permanent storage for data.
   -   Stores operating system, programs, and user files.
   -   Measured in gigabytes (GB) or terabytes (TB).
   -   Hard disk drives (HDDs) are traditional, slower, and cheaper.
   -   Solid-state drives (SSDs) are newer, faster, and more expensive.

**4. Graphics Card (GPU):**
   -   Processes and displays images.
   -   Essential for gaming, video editing, and other graphics-intensive tasks.
   -   Measured in video RAM (VRAM) and clock speed.

**5. Motherboard:**
   -   Connects all the components.
   -   Provides power and communication pathways.

**6. Input/Output (I/O) Devices:**
   -   Allow the user to interact with the computer.
   -   Examples: keyboard, mouse, monitor, printer.

**7. Operating System (OS):**
   -   Software that manages the computer's resources.
   -   Provides a user interface and basic functionality.
   -   Examples: Windows, macOS, Linux.

When you run a program on your computer, the following happens:

1.  The program instructions are loaded from storage into memory.
2.  The processor reads the instructions from memory and executes them one by one.
3.  If the instruction involves calculations, the processor performs them using its arithmetic logic unit (ALU).
4.  If the instruction involves data, the processor reads or writes to memory.
5.  The results of the calculations or data manipulation are stored in memory.
6.  If the program needs to display something on the screen, it sends the necessary data to the graphics card.
7.  The graphics card processes the data and sends it to the monitor, which displays it.

This process continues until the program has completed its task or the user terminates it.

Generierungskonfiguration

Mit dem Argument generation_config können Sie die Generierungsparameter ändern. Jeder Prompt, den Sie an das Modell senden, enthält Parameterwerte, mit denen generiert das Modell Antworten.

model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(
    'Tell me a story about a magic backpack.',
    generation_config=genai.types.GenerationConfig(
        # Only one candidate for now.
        candidate_count=1,
        stop_sequences=['x'],
        max_output_tokens=20,
        temperature=1.0)
)
text = response.text

if response.candidates[0].finish_reason.name == "MAX_TOKENS":
    text += '...'

to_markdown(text)
Once upon a time, in a small town nestled amidst lush green hills, lived a young girl named...

Nächste Schritte

  • Prompt-Entwurf ist der Prozess der Erstellung von Prompts, die die gewünschten von Language Models zu erhalten. Gut strukturierte Prompts sind Ein wesentlicher Bestandteil bei der Gewährleistung korrekter, qualitativ hochwertiger Antworten aus einer bestimmten Sprache modellieren. Informationen zu Best Practices für Prompts Text.
  • Gemini bietet mehrere Modellvarianten für unterschiedliche Anforderungen wie Eingabetypen und -komplexität, Implementierungen für Chat und andere Dialogsprachaufgaben und Größenbeschränkungen. Informationen zu den verfügbaren Gemini-Modelle.