Google Gemini 대형 언어 모델을 활용하기 위한 Gemini API용 파이썬 SDK 사용법
!pip install -q -U google-generativeai
gemini-pro 모델 사용
<시도1>
The meaning of life is a deeply personal and philosophical question that has been pondered by humans for centuries. There is no one definitive answer, and different individuals may have different interpretations. However, some common themes that emerge in discussions about the meaning of life include:
Purpose and Fulfillment: Many people seek a sense of purpose or meaning in their lives. This could involve pursuing personal goals, making a positive impact on others, or contributing to a cause they believe in. Fulfillment often comes from feeling that one is living a life that is aligned with one's values and beliefs.
Connection and Relationships: Strong social connections and relationships are often seen as essential for human well-being. Sharing experiences, providing support, and feeling loved and accepted can give life a sense of meaning.
Growth and Learning: Personal growth and development can also contribute to a sense of meaning. Continuously learning, challenging oneself, and expanding one's perspectives can lead to a deeper understanding of the world and oneself.
Transcendence and Spirituality: For some people, the meaning of life may be connected to a belief in a higher power or a spiritual dimension. Transcending the physical world and connecting with something greater than oneself can provide a sense of purpose and meaning.
Experiencing the Present Moment: While reflecting on the past or planning for the future is important, some argue that true meaning comes from being fully present in the current moment. Savoring experiences, appreciating the beauty of the world, and living without regrets can contribute to a sense of fulfillment.
Contributing to the World: Making a difference in the world, however small, can give life meaning. Whether it's volunteering, helping others, or simply being a kind and compassionate person, contributing to the greater good can foster a sense of purpose.
Finding Meaning in the Journey, Not the Destination: Some believe that the meaning of life is not about achieving a specific goal or destination, but rather about the experiences and lessons learned along the way. The journey itself, with its challenges and triumphs, can provide valuable insights and contribute to a sense of purpose.
Ultimately, the meaning of life is a personal journey and interpretation. It requires reflection, self-awareness, and a willingness to explore different perspectives. There is no right or wrong answer, but by seeking purpose, connection, growth, and meaningfulness, individuals can strive for a fulfilling and purposeful life.
<시도2>
There is no one definitive answer to this question, as it is a deeply personal one that can vary depending on individual beliefs, values, and experiences. However, some common themes that emerge when considering the meaning of life include:
Finding purpose and fulfillment: This involves identifying what brings you joy and satisfaction, and pursuing activities that align with your values and goals. It can also involve making a contribution to the world, whether through your work, relationships, or community involvement.
Connecting with others: Meaningful connections with loved ones, friends, and community members can bring a sense of belonging, support, and purpose. Nurturing these relationships can contribute significantly to your overall well-being.
Growing and learning: Continuously seeking knowledge, developing new skills, and embracing new experiences can stimulate your mind and keep you engaged in life. Personal growth and learning not only add value to your own existence but also benefit those around you.
Making a difference: Using your time, resources, or talents to help others or make a positive impact on the world can give your life a sense of purpose and meaning. Whether it's volunteering, donating to charity, or simply being kind to those around you, acts of compassion can bring fulfillment and purpose.
Finding inner peace: Developing a sense of inner peace and contentment can help you navigate life's challenges and find meaning even in difficult times. Practices such as mindfulness, meditation, or spending time in nature can promote inner calm and well-being.
Embracing the present moment: While it's important to reflect on the past and plan for the future, focusing on and appreciating the present can help you find joy and meaning in each day. Cultivating gratitude and living in the moment can bring a sense of fulfillment and contentment.
Ultimately, the meaning of life is unique to each individual and can evolve over time. It is a personal journey of exploration, self-discovery, and the pursuit of purpose and happiness.
GenerateContentResponse.prompt_feedback
사용response.prompt_feedback
실행 시, 아무것도 출력되지 않음HARM_CATEGORY_SEXUALLY_EXPLICIT : 노골적으로 성적인 경우
HARM_CATEGORY_HATE_SPEECH : 증오 발언의 경우
HARM_CATEGORY_HARASSMENT : 괴롭힘의 경우
HARM_CATEGORY_DANGEROUS_CONTENT : 위험한 소재일 경우
-> NEGLIGIBLE : 무시 가능
+) Gemini는 단일 프롬프트에 여러 반응을 생성할 수 있는데 이 대답들을 cadidates 부름. 가장 적절한 하나의 대답을 선정하기 위해서 살펴볼 수 있음.
+) stream 옵션 : 기본적으로 모델은 전체 생성 프로세스를 완료한 후 응답을 반환함. 생성 중인 응답을 스트리밍할 수 있으며, 생성되는 즉시 모델은 응답 청크를 리턴함. 응답에서부터 청크를 출력해서 확인 가능. 스트리밍을 할 때, 일부 응답 속성은 응답 청크를 통해 반복될 때까지 이용할 수 없음. 이때, IncompleteIterationError 발생.
Gemini는 텍스트와 이미지 및 입력을 모두 수용하는 멀티모달 모델(gemini-provision)을 제공. GenerativeModel.generate_content API는 멀티모달 프롬프트를 처리하도록 설계되었으며 텍스트 출력을 반환.
generate_content
로 모델에 이미지를 전달.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)
Gemini를 사용하여 여러 턴에 걸친 자유로운 형식의 대화 가능. ChatSession 클래스는 대화 상태를 관리하여 과정을 단순화하므로 generate_content와 달리 대화 기록을 목록으로 저장할 필요 없음.
큰 언어 모델에는 컨텍스트 창이 있으며 컨텍스트 길이는 토큰 수로 측정됨. Gemini API를 사용하면 어떤 glm.Content 객체당 토큰 수를 결정할 수 있음.
임베딩은 정보를 배열에서 부동 소수점 숫자의 목록으로 표현하는 데 사용되는 기술. Gemini를 사용하면 텍스트(단어, 문장 및 텍스트 블록)를 벡터화된 형태로 표현할 수 있어 임베딩을 비교하고 대조하기가 더 쉬움. 예를 들어, 유사한 주제 또는 감정을 공유하는 두 텍스트의 임베딩은 유사해야 하며, 이는 코사인 유사성과 같은 수학적 비교 기술을 통해 식별될 수 있음.
RETRIEVAL_QUERY : 쿼리
RETRIEVAL_DOCUMENT : 문서 with 제목
SEMANTIC_SIMILARITY : Semantic Textal Simplicity
CLASSIFICATION : 분류
CLUSTERING : 클러스터링
glm.Content 객체가 멀티모달인 반면, embed_content 메서드는 텍스트 임베딩만 지원. 이 설계는 API를 멀티모달 임베딩으로 확장할 수 있는 가능성 제공.
chat.history도 glm.Content 객체의 목록을 포함하기에 embed_content 함수에 전달 가능.
safety_settings 인수 : 프롬프트와 응답 모두에서 모델이 차단하고 허용하는 내용 구성.
import google.ai.generativelanguage as glm
SDK는 glm.Part 객체 목록을 포함하는 glm.Content에 메세지를 변환하는 시도를 함
텍스트(문자열)
인라인데이터(glm.Blob) 블롭은 바이너리 데이터와 mime_type을 포함
동등한 딕셔너리로 어떠한 클래스든 전달 가능
mime type은 이미지 타임으로도 수용
glm.Content 객체에 전달하여 멀티턴 대화로 처리
append로 메세지를 추가하여 활용 가능
generation_config 인수 : 생성 매개변수 수정
모델에 보내는 모든 프롬프트에는 모델이 대답을 생성하는 방법을 제어하는 매개 변수 값이 포함됨.
ex) candidate_count=1 : 후보 1개로 설정