Nvidia DLI 이수 과정에서 Langchain 실습 내용을 남깁니다. 기본적인 clinet 생성과 call과정을 남깁니다. gemini free tier를 이용했습니다.
전체 코드링크
https://github.com/bellepoque7/nvidia_dli/blob/main/course1.ipynb
nvidia_dli/course1.ipynb at main · bellepoque7/nvidia_dli
Contribute to bellepoque7/nvidia_dli development by creating an account on GitHub.
github.com
1. 환경세팅
from dotenv import load_dotenv
import getpass
import os
load_dotenv(verbose = True)
# os.getenv("GOOGLE_API_KEY")
if "GOOGLE_API_KEY" not in os.environ:
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Google AI API key: ")
https://python.langchain.com/docs/integrations/chat/google_generative_ai/
ChatGoogleGenerativeAI | 🦜️🔗 LangChain
Access Google's Generative AI models, including the Gemini family, directly via the Gemini API or experiment rapidly using Google AI Studio. The langchain-google-genai package provides the LangChain integration for these models. This is often the best star
python.langchain.com
NVIDA에서 모델 불러올 수 있는데 황당한건 모델 컨테이너 사이트에서 한국인증이 아직 안됩니다.. 어째서..?
2. 본문 불러오기
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
result = llm.invoke('What is OMSCS?')
print(result.content)
"""
OMSCS stands for **Online Master of Science in Computer Science** offered by the **Georgia Institute of Technology (Georgia Tech)**.
Here's a breakdown of what makes it significant:
* **Reputable Institution:** It's a fully online master's degree from a top-ranked computer science program. Georgia Tech is highly respected in the field.
* **Affordable:** Compared to traditional on-campus master's programs, OMSCS is significantly more affordable. This makes it accessible to a wider range of students.
* **Flexible:** The online format allows students to study at their own pace and on their own schedule, making it ideal for working professionals.
* **Rigorous Curriculum:** The program covers a wide range of computer science topics and maintains a high academic standard. It is not a "watered down" version of the on-campus program.
* **Diverse Specializations:** Students can choose to specialize in areas such as:
"""
3. 스트리밍과 배치
- invoke와 대응되게 대답을 받는 방법이며, streaming은 동기적으로 batch는 비동기 형식으로 대답을 반환
# llm이 뱉어내는 결과를 스트리밍하게 밷어낼 수 있다.
# invoke는 동기식, stream은 비동기식으로 결과를 받아온다.
prompt = "What is OMSCS?"
for chunk in llm.stream(prompt):
print(chunk.content, end='')
#batch 식
prompts = [
'What is OMSCS?',
'What is the capital of France?',
'What is the largest mammal?'
]
results = llm.batch(prompts)
for result in results:
print(result.content)
4. 템플릿을 이용한 재사용
def translate_to_french(text):
prompt = f"Translate the following English text to French: '{text}'"
return llm.invoke(prompt).content
english_statsments = [
"Hello, how are you?",
"What is your name?",
"I love programming.",
"The weather is nice today."
]
prompts = [translate_to_french(statement) for statement in english_statsments]
prompts
"""
['Here are a few options for translating "Hello, how are you?" into French, with slightly different nuances:\n\n* **Bonjour, comment allez-vous ?** (Formal, polite. Use with people you don\'t know well, or in professional settings.)\n\n* **Salut, comment vas-tu ?** (Informal, friendly. Use with friends, family, and people you know well.)\n\n* **Bonjour, ça va ?** (Informal, but very common. A general greeting, like "Hi, how\'s it going?")\n\n* **Salut, ça va ?** (Even more informal, like "Hey, what\'s up?")\n\nThe best choice depends on the context and your relationship with the person you\'re speaking to.',
'The most common and polite translation of "What is your name?" in French is:\n\n**Comment vous appelez-vous ?**\n\nHere are a few other options, with slightly different connotations:\n\n* **Quel est votre nom ?** (More formal, literally "What is your name?")\n* **Tu t\'appelles comment ?** (Informal, used with people you know or are on familiar terms with)\n* **Comment tu t\'appelles ?** (Also informal, but slightly more direct than "Tu t\'appelles comment ?")\n\nThe best choice depends on the context and your relationship with the person you\'re asking. However, "Comment vous appelez-vous ?" is generally the safest and most appropriate option.',
'The most common and direct translation of "I love programming" in French is:\n\n**J\'adore la programmation.**\n\nHere are a few other options, with slightly different nuances:\n\n* **J\'aime la programmation.** (This is also very common and means "I like programming," but can also imply love in some contexts)\n\n* **J\'aime beaucoup la programmation.** (This emphasizes the liking, "I like programming a lot")\n\n* **Je suis passionné(e) par la programmation.** (This means "I am passionate about programming." Use "passionné" if you are male, and "passionnée" if you are female.)\n\n* **La programmation, c\'est ma passion.** (This means "Programming is my passion.")\n\nThe best choice depends on the specific context and the degree of enthusiasm you want to convey. However, **J\'adore la programmation** is generally the safest and most natural translation for "I love programming."',
'There are a few ways to translate "The weather is nice today" into French, depending on the nuance you want to convey:\n\n* **Il fait beau aujourd\'hui.** (This is the most common and straightforward translation. It means "The weather is good today.")\n\n* **Le temps est agréable aujourd\'hui.** (This is a slightly more formal way of saying it, and means "The weather is pleasant today.")\n\n* **On a du beau temps aujourd\'hui.** (This is a more colloquial way of saying it, and translates to "We have nice weather today.")\n\nSo, the best option depends on the context, but **Il fait beau aujourd\'hui** is generally the safest and most widely understood.']
"""
translations = llm.batch(prompts)
for translation in translations:
print('---')
print(translation.content)
"""
---
This is a great and accurate breakdown of the different ways to say "Hello, how are you?" in French! You've covered the key distinctions of formality and provided helpful context for each option.
Here are a few minor additions that could further enhance this explanation:
"""
5. template 으로 프롬프트 엔지니어링 하기
ChatPromptTemplate 클래스를 이용해서 사전에 프롬프팅을 할 내용을 넣을 수 있고 f-string 형식으로 전달받을 인자를 정할 수 있음
#template 으로 프롬프트 엔지니어링 하기
from langchain_core.prompts import ChatPromptTemplate
#사전에 템플릿을 만들어 runnable을 만드는 방식
english_to_spanish_template = ChatPromptTemplate.from_template("""Translate the following English text to Spanish Provide only the translated text: {english_statement}""")
prompt = english_to_spanish_template.invoke('Hello, how are you?')
print(llm.invoke(prompt).content)
6. 예시
- 다음 문장들을 가지고 3가지(감성분석, 메인 토픽 추출, 다음에 올 질문 만들기)
statements = [
"I had a fantastic time hiking up the mountain yesterday.",
"The new restaurant downtwon serves delicious vegeterian dishes.",
"I am feeling quite stressed about the upcoming project deadline.",
"Watching the sunsset at the beach was a calming exmperience.",
"I recently started reading a fascinating book about space exploration.",
]
sentiment_template = ChatPromptTemplate.from_template("In a single word, either 'positive' or 'negative' provide the overall sentiment of " \
"following piece of text: {text}")
main_topic_template = ChatPromptTemplate.from_template("Identify main topic with one sentence about following piece of text: {text}")
followup_template = ChatPromptTemplate.from_template("what is followup just one question about following piece of text: {text}")
sentiment_prompts = [sentiment_template.invoke({"text":statement}) for statement in statements ]
main_prompts = [main_topic_template.invoke({"text":statement}) for statement in statements ]
followup_prompts = [followup_template.invoke({"text":statement}) for statement in statements ]
sentiments = llm.batch(sentiment_prompts)
main_topics = llm.batch(main_prompts)
followups = llm.batch(followup_prompts)
for statement, sentiment, main_topic, followup in zip(statements, sentiments, main_topics, followups):
print(
f"Statement : {statement}\n"
f"Overall sentiment : {sentiment.content}\n"
f"Main topic : {main_topic.content}\n"
f"Followup question : {followup.content}"
)
'Data Science > LLM' 카테고리의 다른 글
Prompt Engineering DLI course 후기와 AI assistant로 학습 부스팅하기 (0) | 2025.06.27 |
---|---|
프롬프트 엔지니어링 with NVIDIA DLI Course (1) | 2025.06.26 |