ai.google.dev에서 보기 | Colab 노트북 사용해 보기 | GitHub에서 노트북 보기 |
PaLM API를 빠르게 사용해 보려면 curl 명령어를 사용하여 REST API에서 메서드를 호출합니다. 다음 예 각 API 메서드에 대한 호출을 보여줍니다. 각 curl 명령어에 대해 모델 이름과 API 키입니다 API 키 가져오기를 참고하세요. 아직 없다면 이 페이지를 참고하세요.
"""
Add your API key and run this cell to set the PALM_API_KEY variable if
running in Colab.
(In a terminal you can just run PALM_API_KEY=<your API key>)
"""
import os
os.environ['PALM_API_KEY'] = ""
텍스트 생성
generate_text
메서드 사용
모델을 사용하여
응답을 생성합니다
!curl https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=$PALM_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{ \
"prompt": { \
"text": "Write a story about a magic backpack." \
} \
}'
{ "candidates": [ { "output": "Once upon a time, there was a young girl named Lily who loved to explore. She would spend hours wandering through the forest near her house, looking for new and exciting things. One day, Lily was exploring a particularly dense part of the forest when she came across a strange backpack. It was sitting on a rock, and it looked like it had been abandoned.\n\nLily picked up the backpack and examined it. It was made of a strange, silvery material that she had never seen before. There were no zippers or buttons, but the backpack seemed to be open. Lily reached inside, and she was surprised to find that it was full of all sorts of amazing things.\n\nThere were books that could tell her anything she wanted to know, tools that could fix anything she broke, and clothes that could keep her warm or cool no matter what the weather. Lily was amazed by the backpack's contents, and she knew that she had found something truly special.\n\nLily decided to take the backpack home with her, and she quickly discovered that it was even more magical than she had thought. The backpack could transport her to any place she wanted to go, and it could provide her with anything she needed. Lily used the backpack to travel the world, and she had many amazing adventures.\n\nOne day, Lily was using the backpack to explore a new planet when she came across a group of people who were in trouble. The people were being held captive by a cruel wizard, and Lily knew that she had to help them. She used the backpack to create a distraction, and then she rescued the people and led them to safety.\n\nThe people were so grateful to Lily for saving them, and they told her that she was a true hero. Lily was happy to have helped, and she knew that she would continue to use the backpack to help others in need.\n\nLily's magic backpack continued to accompany her on many more adventures. She used it to help people in need, to explore new worlds, and to learn about all sorts of amazing things. Lily's backpack was a constant source of joy and inspiration, and she was grateful for the many ways that it had enriched her life.", "safetyRatings": [ { "category": "HARM_CATEGORY_DEROGATORY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_TOXICITY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_VIOLENCE", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_MEDICAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS", "probability": "NEGLIGIBLE" } ] } ] }
다음 예는
generate_text
메서드를 사용하여 지도 가장자리에
패딩을 추가할 수 있습니다.
!curl https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=$PALM_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{ \
"prompt": { \
"text": "Give an example of a title for a story about a magic backpack. \
Title: " \
}, \
"safetySettings": [ \
{ \
"category": "HARM_CATEGORY_TOXICITY", \
"threshold": "BLOCK_ONLY_HIGH" \
} \
], \
"stopSequences": [ \
"Title" \
], \
"temperature": 1.0, \
"candidate_count": 3, \
"maxOutputTokens": 800, \
"topP": 0.8, \
"topK": 10 \
}'
{ "candidates": [ { "output": "The Backpack of Holding", "safetyRatings": [ { "category": "HARM_CATEGORY_DEROGATORY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_TOXICITY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_VIOLENCE", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_MEDICAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS", "probability": "NEGLIGIBLE" } ] }, { "output": "The Backpack of Holding", "safetyRatings": [ { "category": "HARM_CATEGORY_DEROGATORY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_TOXICITY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_VIOLENCE", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_MEDICAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS", "probability": "NEGLIGIBLE" } ] }, { "output": "The Backpack of Holding", "safetyRatings": [ { "category": "HARM_CATEGORY_DEROGATORY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_TOXICITY", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_VIOLENCE", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_SEXUAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_MEDICAL", "probability": "NEGLIGIBLE" }, { "category": "HARM_CATEGORY_DANGEROUS", "probability": "NEGLIGIBLE" } ] } ] }
매개변수 값의 의미는 다음과 같습니다.
safetySettings
: 유해성의 안전성 기준점을 낮춰 악의적일 가능성이 높은 콘텐츠만 차단 출력stopSequences
: 정지 시퀀스를 사용하여 다음 위치에서 출력을 중지하여 제목을 1개만 생성합니다.Title
의 다음 인스턴스입니다.temperature
: 응답을 생성할 때 온도를 1.0으로 설정합니다.candidate_count
: 후보 응답 3개만 반환합니다.maxOutputTokens
: 최대 800개의 토큰을 반환합니다.topP
: 모델이 반환한 상위 80% 의 토큰만 고려합니다.topK
: 샘플링 시 상위 10개 토큰을 고려합니다.
API 참조에서 이러한 매개변수에 대해 자세히 알아보기
generateText
관련
메서드를 사용하여 축소하도록 요청합니다.
메시지 생성
generateMessage
사용
모델을 사용하여 응답을 생성하는 방법
(입력 MessagePrompt
)
!curl https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:generateMessage?key=$PALM_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{ \
"prompt": {"messages": [{"content":"hi"}]} \
}'
{ "candidates": [ { "author": "1", "content": "Hi! How can I help you today?" } ], "messages": [ { "author": "0", "content": "hi" } ] }
다음 예는 매개변수에 다른 값을 사용한 호출을 보여줍니다.
!curl https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:generateMessage?key=$PALM_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{ \
"prompt": {"messages": [{"content":"hi"}]}, \
"temperature": 0.1, \
"candidate_count": 1, \
"topP": 0.8, \
"topK": 10}'
{ "candidates": [ { "author": "1", "content": "Hi! How can I help you today?" } ], "messages": [ { "author": "0", "content": "hi" } ] }
매개변수 값의 의미는 다음과 같습니다.
temperature
: 응답을 생성할 때 온도를 0.1로 설정합니다.candidate_count
: 후보 응답 1개만 반환합니다.topP
: 모델이 반환한 상위 80% 의 토큰만 고려합니다.topK
: 샘플링 시 상위 10개 토큰을 고려합니다.
API 참조에서 이러한 매개변수에 대해 자세히 알아보기
generateMessage
관련
메서드를 사용하여 축소하도록 요청합니다.
텍스트 삽입
embedText
메서드를 사용하여 다음을 수행합니다.
모델에서 임베딩을 생성합니다.
!curl https://generativelanguage.googleapis.com/v1beta2/models/embedding-gecko-001:embedText?key=$PALM_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{"text": "say something nice!"}'
{ "embedding": { "value": [ 0.0011238843, -0.040586308, -0.013174802, 0.015497498, 0.04383781, 0.012527679, 0.017876161, 0.031339817, -0.0042566974, -0.024129443, -0.023050068, -0.015625203, 0.03501345, -0.006033779, -0.011984176, -0.033368077, -0.040653296, 0.022117265, -0.02034076, -0.040752005, -0.12748374, 0.029760985, 0.00084632, -0.017502416, -0.03893842, -0.07151896, 0.0609997, -0.0046266303, -0.044301335, -0.022592714, 0.023920823, 0.0020489343, -0.0048049283, -0.038431767, 0.007275116, 0.018562535, 0.017131427, -0.00043720857, 0.02810143, 0.053296003, 0.031831037, -0.067091785, 0.015640317, -0.0036152988, -0.04691379, -0.044070054, -0.022364588, -0.0083763655, -0.0490714, -0.007302964, 0.006516001, -0.004685413, 0.03979989, -0.014196505, 0.01065721, -0.0073698894, -0.036348466, -0.008763353, -0.01892632, -0.054501593, 0.032021806, -0.007242739, 0.0220439, -0.07687204, -0.0740849, 0.01748987, -0.027381063, -0.015533608, -0.013165218, -0.04867313, 0.041797243, 0.017989235, -0.00982055, 0.03691631, 0.010164966, -0.03985747, 0.024958102, 0.015761184, 0.02152081, -0.06986678, -0.012039569, 0.00056548475, -0.030969337, -0.07435745, -0.028182292, 0.012739926, 0.042157806, 0.023305222, -0.03230193, -0.0033747193, -0.061240233, 0.021881713, 0.009969781, -0.010255637, -0.0049942187, -0.034989387, 0.020215971, -0.020086009, 0.0010875552, 0.017283859, 0.0008415358, -0.052446693, 0.019897413, 0.010148648, 0.039659496, 0.010298634, 0.033420425, 0.043799188, -0.072159335, -0.06268149, -0.06190123, 0.049205165, -0.054376606, -0.016845694, 0.0365771, -0.037944548, 0.006270467, -0.013075792, 0.099409916, 0.030545607, -0.0732958, -0.033732962, 0.0044321134, -0.0013212749, 0.031492565, -0.014452682, 0.058129285, -0.020901497, 0.00432678, -0.014602101, 0.028027585, 0.04006542, 0.03904733, -0.041794587, -0.023020804, 0.05953402, 0.00020820377, -0.04229413, 0.0012092285, 0.035000943, -0.06702403, -0.012794849, 0.0036005897, 0.023782397, 0.014083849, -0.0036575894, 0.028294614, -0.021102497, -0.011178294, -0.0742226, -0.01762662, -0.034421157, 0.00959854, 0.030744651, 0.03575221, 0.03232364, 0.012205379, 0.0849699, -0.04682018, -0.027969383, 0.008350599, -0.002548684, 0.011669425, -0.028728798, 0.0085435, 0.0021975278, 0.03761783, -0.03241707, 0.023464072, 0.040996246, -0.001109026, -0.13223213, -0.020976847, 0.015984828, 0.09512107, -0.010276377, 0.014994116, 0.032845948, -0.034293175, 0.02430849, -0.009919951, -0.072498284, 0.026591707, 0.0015673019, -0.045289494, 0.021523945, 0.01088207, 0.006588061, -0.048151653, 0.05282844, 0.008129585, 0.027846182, 0.018659458, -0.022406245, -0.043990742, 0.017680017, 0.013181173, -0.14280352, 0.0021016353, 0.045783103, 0.0037692375, -0.039056912, 0.0116640935, 0.013924913, 0.020162055, -0.034111485, -0.080292016, 0.03685934, -0.0071554463, -0.011398124, -0.0021998466, -0.04018377, 0.10160932, 0.021735929, -0.018394442, -0.0042295344, -0.025801938, -0.036239583, -0.044041224, -0.045289584, 0.0014948259, 0.010338574, 0.024361573, -0.031958524, 0.024407288, -0.011137719, 0.0043831025, 0.014189889, 0.024169667, 0.0022451929, -0.035389382, 0.024017807, 0.01553733, -0.0473291, 0.026909798, 0.055664524, 0.03976394, -0.010415778, -0.06393292, 0.01792164, 0.029561834, 0.07391933, 0.0022118469, 0.008295304, -0.05163188, 0.010020474, -0.012885397, 0.019830104, 0.028650315, -0.013769379, 0.0338601, -0.06765002, 0.040466607, -0.010720626, 0.012154494, 0.013327016, -0.010665587, 0.037299912, 0.04323995, -0.0009342932, 0.033894684, -0.0042288457, -0.0052715484, 0.14659505, -0.063753605, -0.04746297, -0.035064638, -0.0011817935, 0.05915483, 0.031045768, -0.040024273, 0.017579468, 0.05911642, 0.05578586, 0.014392062, 0.034441307, 0.0051881303, 0.038473494, 0.023593616, 0.034369726, -0.012554688, -0.026979957, -0.008302676, 0.045971826, 0.016231172, 0.0104582505, -0.03975078, 0.016210284, -0.0496326, -0.0055911625, 0.013650961, -0.0144314915, 0.004787867, 0.036809307, 0.012353121, 0.015239806, 0.049226765, -0.028741457, -0.061572257, 0.039226513, 0.016116843, -0.024787322, -0.02130387, -0.03779633, -0.06941293, -0.049595475, 0.029587833, 0.033705294, 0.032990646, -0.05216195, 0.029583171, -0.0056521366, 0.021131132, -0.0056709587, -0.044883735, 0.025333345, 0.024979891, -0.003277763, -0.09892439, 0.03376726, -0.05836421, 0.004832982, 0.1255239, -0.008487428, 0.021888541, -0.021681873, -0.012966817, -0.028696185, 0.012639684, -0.05412777, 0.01192876, 0.034647945, -0.056298435, 0.024278637, 0.00871971, 0.090212576, 0.006437413, 0.037434887, 0.00019946751, -0.13568601, 0.028605944, -0.02006474, -0.025272185, -0.083530694, -0.04269899, 0.014648017, -0.0010991616, -0.031067085, -0.050440468, 0.00091479445, 0.0015937795, 0.045591757, -0.006403704, 0.014939055, 0.02255195, 0.027493088, -0.0012262427, 0.023612592, -0.003841696, 0.04333225, 0.020669367, 0.033022266, 0.017286869, 0.017276619, -0.009199664, 0.015230595, -0.06797182, 0.014121894, 0.013572196, -0.029916016, 0.0040522343, -0.029725965, -0.0102597, -0.013439138, -0.08198655, -0.0070174187, 0.004431028, 0.011755264, 0.0072572646, 0.037631817, -0.0051535456, -0.0081493175, -0.015169597, 0.0055189617, -0.005826631, -0.020261835, 0.083080135, 0.064882986, -0.010988733, -0.026083246, -0.015315506, -0.0015296342, 0.0059038294, -0.016090974, -0.02000143, 0.074654035, -0.008555022, 0.016888265, -0.016842794, -0.044189937, 0.019509379, -0.019198475, -0.003341484, -0.03456671, -0.0052166893, -0.023366041, -0.005331629, 0.023549462, 0.0015763871, -0.025385149, 0.019852458, -0.031065127, 0.0017880594, -0.0026873888, 0.030643448, -0.031892557, -0.079189695, -0.026351755, 0.0007078425, -0.01158725, 0.051290438, 0.038942136, -0.017992912, 0.028203778, 0.025912454, 0.015449621, -0.0447461, 0.035019837, 0.000101277845, -0.043222122, 0.008197146, -0.0072293375, -0.0016813442, -0.003254859, -0.046821237, -0.010909567, 0.12034104, 0.047507685, 0.0004942198, -0.0061685564, -0.041621834, 0.0016361383, -0.029424895, -0.027021525, 0.029043453, -0.036468945, -0.018375812, -0.03730402, -0.04374344, -0.017010607, -0.02513565, 0.0403428, 0.038378354, 0.05564215, 0.047473494, -0.0016713048, -0.0083524, -0.04078778, 0.006816706, 0.026119364, -0.033208948, -0.028723165, -0.028907888, 0.03388191, -0.027172018, -0.0056449105, 0.0076403813, -0.017191296, 0.0103864325, 0.039782897, -0.024052272, -0.000900677, -0.008114914, 0.009936376, 0.057062533, 0.015691994, 0.06488037, 0.05861413, 0.020779617, -0.0247915, -0.03869506, -0.0018747158, -0.018278932, -0.08014962, 0.054706935, -0.0061153383, 0.0060715703, -0.027196469, 0.011381146, 0.043631904, 0.04475186, 0.040986426, 0.004848564, -0.0066069923, 0.021026906, -0.021832775, 0.044528317, -0.0024687534, -0.022941994, 0.063197434, 0.028125852, 0.023551032, -0.0058099683, -0.012617737, -0.012245235, 0.026610427, -0.009620305, -0.009891919, -0.044978708, 0.01857481, -0.045637384, 0.04385301, 0.022688234, -0.03197118, -0.017255962, -0.0971544, -0.034807537, 0.050930932, -0.004510808, 0.018908406, 0.04590128, -0.0431137, 0.030199846, 0.07177168, 0.034885082, 0.031304233, 0.015819319, -0.044044714, -0.035496857, 0.01251333, -0.011556127, 0.050805565, 0.022510093, 0.024378778, -0.008928189, -0.035410564, 0.07907622, 0.0077974293, 0.08509954, 0.057877623, -0.02150467, -0.031975858, 0.031035855, 0.008521942, 0.0045759548, 0.018532533, -0.013495335, 0.027427813, 0.06354547, 0.03725446, -0.014155721, -0.07390833, -0.07398356, 0.049747474, 0.05974928, 0.03418353, -0.04185207, -0.048303224, -0.02520631, -0.032343563, -0.008572907, -0.04996052, -0.020948416, -0.017813088, 0.018417805, -0.05610268, 0.07127963, 0.044351287, -0.008157627, 0.025784992, -0.010244056, -0.049246937, -0.053404044, -0.004473588, 0.021395257, 0.012865255, 0.008757349, 0.025809702, 0.024504552, -0.008121683, -0.010630886, -0.0282137, 0.0131343175, 0.025023215, -0.054416455, -0.00987844, -0.05275506, -0.046944536, 0.012130432, -0.054224152, -0.04411418, -0.056159925, -0.0301649, -0.009835849, -0.03362433, -0.049971923, -0.008385748, -0.019473836, 0.023564048, 0.008061396, 0.0053569255, 0.019741505, 0.025652621, -0.040649947, 0.028353963, 0.033970434, 0.027768575, 0.04150317, 0.07797273, 0.0059470637, 0.016306393, 0.048800167, 0.04042705, -0.0005679075, -0.018945955, -0.0018624268, -0.050942365, 0.050798208, 0.021266282, -0.0017024949, 0.03493305, 0.014065046, -0.029225372, -0.028919723, -0.014542683, -0.0010159526, 0.0031558631, 0.026344743, -0.015071228, -0.025507499, 0.021337807, -0.0067190765, -0.04575622, -0.047282696, 0.03300356, -0.032847676, 0.04183392, 0.002707342, 0.048737124, 0.026989447, 0.005501418, 0.0005985079, -0.013466379, -0.011694075, -0.020797541, 0.013499941, -0.0013224268, -0.06625725, -0.028281163, 0.03480317, 0.015610069, -0.028201189, -0.055121798, -0.013768992, -0.013169174, 0.04281197, -0.022435848, 0.04519679, -0.011604503, 0.03215822, 0.019064883, 0.028547175, 0.0070973705, 0.030223792, -0.096697725, 0.037206784, 0.014219172, 0.025670564, 0.007972781, 0.039132267, -0.01641151, -0.053440955, -0.014801056, 0.009477607, -0.037399042, 0.017024228, 1.1309146e-06, 0.017277671, -0.014029856, 0.034332614, 0.008504508, -0.009390249, -0.0020580885, -0.04837862, -0.008487879, -0.0509186, -0.01001286, -0.015390811, -0.02409495, -0.01521604, -0.03393024, 0.0037305295, 0.0003564763, -0.026124911, -0.0011751048, -0.0892414, -0.025555352, 0.028373795, -0.023148732, 0.037046663, -0.030026339, 0.034503758, 0.06984921, 0.020804616, 0.028130436, -0.06043828, -0.017702322, 0.02253427, 0.0023957205, -0.015689885, -0.015208743, -0.005363971, 0.030644067, -0.017427318, -0.011243967, 0.012137882, 0.0005420408, 0.025897957, -0.0152362455, -0.029343305, -0.02368195, 0.049701642, 0.07361216, -0.038200952, -0.06681024, -0.03334393, 0.02077101, -0.02953543, -0.035056386, 0.00046944866, -0.0034510305, -0.030171316, 0.013363371, 0.048995305, -0.003263969, -0.021443684, 0.01924834, -0.004079115, 0.04140935, -0.10210643, -0.047827926, 0.013966705, 0.0073507936, -0.013666492, 0.030878609, 0.019740991, -0.005346492, -0.04305278, -0.0082011735, -0.0664176, -0.051891983, 0.00014011975, 0.042076, 0.039630715, -0.027207172, 0.062501706, -0.014362662, 0.007815066, -0.009666608, 0.03339132, -0.00015506323, 0.025511535, 0.028007986, -0.001376217, 0.0070980764, -0.017834678, -0.04638408, -0.03356212 ] } }
메시지 토큰 수 계산
사용
countMessageTokens
드림
메시지에서 모델의 tokenizer를 실행하는 메서드
프롬프트 문자열을 만들고 토큰 카운트를 가져옵니다.
!curl https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:countMessageTokens?key=$PALM_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{ \
"prompt": { \
"messages": [ \
{"content":"How many tokens?"}, \
{"content":"For this whole conversation?"} \
] \
} \
}'
{ "tokenCount": 23 }
모델 가져오기
get
메서드를 사용하여 다음과 같은 특정 모델에 대한 정보를 가져옵니다.
버전, 표시 이름, 입력 토큰 제한 등
curl https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001?key=$PALM_API_KEY
{ "name": "models/text-bison-001", "version": "001", "displayName": "Text Bison", "description": "Model targeted for text generation.", "inputTokenLimit": 8196, "outputTokenLimit": 1024, "supportedGenerationMethods": [ "generateText" ], "temperature": 0.7, "topP": 0.95, "topK": 40 }
모델 나열
list
메서드를 사용하여
PaLM API입니다.
curl https://generativelanguage.googleapis.com/v1beta2/models?key=$PALM_API_KEY
{ "models": [ { "name": "models/chat-bison-001", "version": "001", "displayName": "Chat Bison", "description": "Chat-optimized generative language model.", "inputTokenLimit": 4096, "outputTokenLimit": 1024, "supportedGenerationMethods": [ "generateMessage", "countMessageTokens" ], "temperature": 0.25, "topP": 0.95, "topK": 40 }, { "name": "models/text-bison-001", "version": "001", "displayName": "Text Bison", "description": "Model targeted for text generation.", "inputTokenLimit": 8196, "outputTokenLimit": 1024, "supportedGenerationMethods": [ "generateText" ], "temperature": 0.7, "topP": 0.95, "topK": 40 }, { "name": "models/embedding-gecko-001", "version": "001", "displayName": "Embedding Gecko", "description": "Obtain a distributed representation of a text.", "inputTokenLimit": 1024, "outputTokenLimit": 1, "supportedGenerationMethods": [ "embedText" ] } ] }