튜토리얼: Gemini API 시작하기


Google AI에서 보기 Google Colab에서 실행 GitHub에서 소스 보기

이 빠른 시작에서는 Gemini API용 Python SDK를 사용하는 방법을 보여줍니다. 를 통해 Google의 Gemini 대규모 언어 모델에 액세스할 수 있습니다. 이 빠른 시작에서는 다음 작업을 수행하는 방법을 배우게 됩니다.

  1. Gemini를 사용하기 위한 개발 환경 및 API 액세스를 설정합니다.
  2. 텍스트 입력에서 텍스트 응답을 생성합니다.
  3. 멀티모달 입력 (텍스트 및 이미지)에서 텍스트 응답을 생성합니다.
  4. 멀티턴 대화에 Gemini를 사용하세요 (채팅).
  5. 대규모 언어 모델에 임베딩을 사용합니다.

기본 요건

이 빠른 시작은 Google Colab 이 노트북을 브라우저에서 직접 실행하며 구성할 수 있습니다

또는 이 빠른 시작을 로컬에서 완료하려면 개발 환경이 환경은 다음 요구사항을 충족합니다.

  • Python 3.9 이상
  • 노트북 실행을 위한 jupyter 설치

설정

Python SDK 설치

Gemini API용 Python SDK는 google-generativeai 패키지를 설치합니다. pip를 사용하여 종속 항목을 설치합니다.

pip install -q -U google-generativeai

패키지 가져오기

필요한 패키지를 가져옵니다.

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 키 설정

Gemini API를 사용하려면 먼저 API 키를 받아야 합니다. 만약 없는 경우 Google AI Studio에서 클릭 한 번으로 키를 만듭니다.

API 키 가져오기

Colab에서 'recommended' 아래의 보안 비밀 관리자에 키를 추가하세요. 왼쪽 패널에 표시됩니다. 이름을 GOOGLE_API_KEY로 지정합니다.

API 키를 확보하면 이를 SDK에 전달합니다. 여기에는 두 가지 방법이 있습니다.

  • 키를 GOOGLE_API_KEY 환경 변수에 입력합니다. SDK는 자동으로 가져옵니다.)
  • 키를 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)

모델 나열

이제 Gemini API를 호출할 준비가 되었습니다. list_models 앱을 사용하여 사용 가능한 Gemini 모델:

  • gemini-1.5-flash: 가장 빠른 멀티 모달 모델
  • gemini-1.5-pro: Google의 가장 강력하고 지능적인 멀티모달 모델
for m in genai.list_models():
  if 'generateContent' in m.supported_generation_methods:
    print(m.name)

텍스트 입력에서 텍스트 생성

텍스트 전용 프롬프트의 경우 Gemini 1.5 모델 또는 Gemini 1.0 Pro 모델을 사용하세요.

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

generate_content 메서드는 다음을 비롯한 다양한 사용 사례를 처리할 수 있습니다. 멀티턴 채팅 및 멀티모달 입력을 지원합니다. 지원합니다 사용 가능한 모델은 입력으로 텍스트와 이미지만 지원하고 출력됩니다.

가장 간단한 경우 프롬프트 문자열을 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

간단한 경우에는 response.text 접근자만 있으면 됩니다. 표시 대상 형식이 지정된 마크다운 텍스트를 사용하려면 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.

API가 결과를 반환하지 못한 경우 다음을 사용합니다. GenerateContentResponse.prompt_feedback 프롬프트와 관련된 안전 문제로 인해 차단되었는지 확인합니다.

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는 단일 프롬프트에 대해 가능한 여러 대답을 생성할 수 있습니다. 이러한 가능한 응답은 candidates라고 하며, 이러한 응답을 검토하여 가장 적합한 응답을 선택합니다

응답 후보 보기 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
  }
]

기본적으로 모델은 전체 생성이 완료된 후 응답을 반환합니다. 프로세스입니다 응답을 생성하는 동안 스트리밍할 수도 있습니다. 모델이 생성되는 즉시 응답 청크를 반환합니다.

응답을 스트리밍하려면 GenerativeModel.generate_content(..., stream=True)를 사용합니다.

%%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.
________________________________________________________________________________

스트리밍할 때 반복할 때까지 일부 응답 속성을 사용할 수 없습니다. 모든 응답 청크를 검토합니다. 이에 대한 설명은 아래에 나와 있습니다.

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

prompt_feedback 속성은 다음과 같이 작동합니다.

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
}

하지만 text와 같은 속성은 다음을 수행하지 않습니다.

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()`)

이미지 및 텍스트 입력에서 텍스트 생성

Gemini는 멀티모달 입력을 처리할 수 있는 다양한 모델을 제공합니다 (Gemini 1.5). 텍스트 및 이미지를 모두 입력할 수 있습니다. 이 프롬프트의 이미지 요구사항을 참고하세요.

프롬프트 입력에 텍스트와 이미지가 모두 포함된 경우 Gemini 1.5를 GenerativeModel.generate_content 메서드를 사용하여 텍스트 출력을 생성합니다.

이미지를 포함해 보겠습니다.

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

Gemini 1.5 모델을 사용하고 generate_content가 있는 모델에 이미지를 전달합니다.

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.

프롬프트에 텍스트와 이미지를 모두 제공하려면 문자열이 포함된 목록을 전달하세요. 이미지:

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!

Chat 대화

Gemini를 사용하면 여러 차례에 걸쳐 자유 형식으로 대화를 나눌 수 있습니다. 이 ChatSession 클래스는 상태를 관리하여 프로세스를 간소화합니다. 따라서 generate_content와 달리 대화 기록을 목록으로 표시합니다.

채팅을 초기화합니다.

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

ChatSession.send_message 메서드는 다음과 동일한 GenerateContentResponse 유형을 반환합니다. GenerativeModel.generate_content입니다. 또한 채팅 기록에 사용자의 메시지와 응답이 추가됩니다.

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"
]

메시지를 계속 보내 대화를 계속할 수 있습니다. 사용 stream=True 인수를 사용하여 채팅을 스트리밍합니다.

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 객체에는 각각 다음을 포함하는 glm.Part 객체 목록이 포함됩니다. text (문자열) 또는 inline_data (glm.Blob) - blob에 바이너리가 포함됨 데이터 및 mime_type. 채팅 기록은 glm.Content 목록으로 확인할 수 있습니다. 객체 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.

토큰 수 계산

대규모 언어 모델에는 컨텍스트 윈도우가 있으며 컨텍스트 길이는 종종 토큰의 수로 측정됩니다. Gemini API를 사용하면 genai.protos.Content 객체당 토큰 수를 결정합니다. 쿼리 문자열을 GenerativeModel.count_tokens 메서드를 다음과 같이 바꿉니다.

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

마찬가지로 token_count에서 ChatSession를 확인할 수 있습니다.

model.count_tokens(chat.history)
total_tokens: 501

임베딩 사용

임베딩 정보를 부동 소수점 수 목록으로 표현하는 데 사용되는 기법입니다. 배열에 포함됩니다. Gemini를 사용하면 텍스트 (단어, 문장, 블록)를 벡터화된 형식으로 더 쉽게 비교 및 대조할 수 있습니다. 임베딩을 학습합니다 예를 들어 주제가 비슷한 두 텍스트 또는 감정에는 유사한 임베딩이 있어야 하며, 이는 임베딩을 통해 코사인 유사성과 같은 수학적 비교 기법입니다. 자세히 알아보려면 임베딩을 사용해야 하는 이유는 임베딩 가이드를 참조하세요.

임베딩을 생성하려면 embed_content 메서드를 사용합니다. 메서드는 삽입해야 합니다 (task_type).

할 일 유형 설명
RETRIEVAL_QUERY 지정된 텍스트가 검색/가져오기 설정의 쿼리임을 지정합니다.
RETRIEVAL_DOCUMENT 지정된 텍스트가 검색/가져오기 설정의 문서임을 지정합니다. 이 작업 유형을 사용하려면 title가 필요합니다.
SEMANTIC_SIMILARITY 지정된 텍스트를 시맨틱 텍스트 유사성(STS)에 사용하도록 지정합니다.
분류 임베딩이 분류에 사용되도록 지정합니다.
클러스터링 클러스터링에 임베딩을 사용하도록 지정합니다.

다음은 문서 검색을 위해 단일 문자열에 대한 임베딩을 생성합니다.

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]

문자열 배치를 처리하려면 content에 문자열 목록을 전달합니다.

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 ...

genai.embed_content 함수는 문자열이나 문자열 목록을 허용하지만 실제로 genai.protos.Content 유형 (예: GenerativeModel.generate_content)을 클릭합니다. glm.Content 객체는 API에서 대화의 기본 단위입니다.

genai.protos.Content 객체는 멀티모달이지만 embed_content는 메서드는 텍스트 임베딩만 지원합니다. 이러한 설계는 API에 가능성이 있습니다.

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 ...

마찬가지로 채팅 기록에는 genai.protos.Content 객체 목록이 포함됩니다. embed_content 함수에 직접 전달할 수 있습니다.

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...

고급 사용 사례

다음 섹션에서는 Gemini API용 Python SDK입니다.

안전 설정

safety_settings 인수를 사용하면 모델이 차단하고 생성하는 항목과 프롬프트와 대답에서 모두 가능합니다 기본적으로 안전 설정은 콘텐츠를 차단합니다. 모든 서비스에서 안전하지 않은 콘텐츠가 중간 또는 높은 확률로 측정기준에 따라 달라집니다. 안전에 대해 자세히 알아보기 설정을 참조하세요.

의심스러운 프롬프트를 입력하고 기본 안전 설정으로 모델을 실행합니다. 추천 항목이 반환되지 않습니다.

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에 메시지를 차단한 안전 필터가 표시됩니다.

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
}

이제 새로 구성된 안전 설정이 포함된 모델에 동일한 프롬프트를 제공합니다. 응답을 받을 수 있습니다

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

또한 프롬프트가 표시되는 경우를 대비하여 각 후보자에는 고유한 safety_ratings가 있습니다. 통과하지만 개별 응답이 안전 검사를 통과하지 못합니다.

메시지 인코딩

이전 섹션에서는 메시지를 쉽게 전송할 수 있도록 SDK를 사용했습니다. API에 전달합니다. 이 섹션에서는 앞서 살펴본 예를 들어 계정 구성과 관련된 하위 수준의 세부정보를 SDK는 메시지를 인코딩합니다.

SDK는 메시지를 genai.protos.Content 객체로 변환하려고 시도합니다. 여기에는 각각 다음 중 하나를 포함하는 genai.protos.Part 객체 목록이 포함됩니다.

  1. text (문자열)
  2. inline_data (genai.protos.Blob) - blob에 바이너리 datamime_type
  3. 기타 유형의 데이터가 포함됩니다

이러한 클래스를 동등한 사전으로 전달할 수도 있습니다.

따라서 이전 예와 완전히 입력된 형식은 다음과 같습니다.

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] ...

멀티턴 대화

앞에서 설명한 genai.ChatSession 클래스는 많은 사용 사례를 처리할 수 있지만 몇 가지 가정을 합니다 사용 사례가 채팅에 적합하지 않은 경우 genai.ChatSession는 래퍼일 뿐이라는 점을 기억하는 것이 좋습니다. 주변 GenerativeModel.generate_content 또한 단일 요청 외에도 멀티턴 대화를 처리할 수 있습니다.

개별 메시지는 genai.protos.Content 객체이거나 호환됩니다. 사전의 내용을 복사합니다. 메시지는 사전으로 에는 roleparts 키가 필요합니다. 대화의 role은 다음 중 하나일 수 있습니다. 프롬프트를 제공하는 user 또는 응답을 제공하는 model

genai.protos.Content 객체 목록을 전달하면 멀티턴 채팅:

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!

대화를 계속하려면 대답과 다른 메시지를 추가하세요.

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.

생성 구성

generation_config 인수를 사용하면 생성 매개변수를 수정할 수 있습니다. 모델로 보내는 모든 프롬프트에는 모델이 응답을 생성합니다.

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...

다음 단계

  • 프롬프트 설계는 원하는 행동을 유도하는 프롬프트를 만드는 프로세스입니다 학습합니다. 체계적인 프롬프트를 작성하는 것은 모든 언어에서 정확하고 높은 품질의 응답을 보장하는 데 있어 있습니다. 프롬프트 권장사항 알아보기 쓰기
  • Gemini는 다양한 용도의 니즈에 맞는 여러 가지 모델 변형을 제공합니다. 입력 유형 및 복잡성, 채팅 또는 기타 대화상자 언어 작업, 크기 제약 조건을 고려해야 합니다. 사용 가능한 Gemini 모델.