Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Ce guide explique comment migrer votre code Python de l'API PaLM à
l'API Gemini. Vous pouvez générer des conversations textuelles et des conversations multitours (chat)
avec Gemini, mais pensez à vérifier vos réponses, car elles peuvent
des sorties PaLM.
Résumé des différences entre les API
Les noms des méthodes ont changé. Au lieu d'avoir des méthodes distinctes pour générer
texte et chat, une méthode generate_content peut faire les deux.
Chat dispose d'une méthode d'assistance start_chat qui facilite la discussion.
Au lieu de fonctions autonomes, les nouvelles API sont des méthodes du
GenerativeModel.
La structure de réponse de sortie a été modifiée.
Les catégories de paramètres de sécurité ont changé. Consultez le
paramètres de sécurité.
Génération de texte : Basic
PaLM
Gemini
pipinstallgoogle-generativeaiimportgoogle.generativeaiaspalmimportospalm.configure(api_key=os.environ['API_KEY'])response=palm.generate_text(prompt="The opposite of hot is")print(response.result)# 'cold.'
pipinstallgoogle-generativeaiimportgoogle.generativeaiasgenaiimportosgenai.configure(api_key=os.environ['API_KEY'])model=genai.GenerativeModel(model_name='gemini-pro')response=model.generate_content('The opposite of hot is')print(response.text)# The opposite of hot is cold.'
Génération de texte : paramètres facultatifs
PaLM
Gemini
pipinstallgoogle-generativeaiimportgoogle.generativeaiaspalmimportospalm.configure(api_key=os.environ['API_KEY'])prompt="""You are an expert at solving wordproblems.Solve the following problem:I have three houses, each with threecats. Each cat owns 4 mittens, and a hat.Each mitten was knit from 7m of yarn,each hat from 4m. How much yarn wasneeded to make all the items?Think about it step by step, and showyour work."""completion=palm.generate_text(model=model,prompt=prompt,temperature=0,# The maximum length of responsemax_output_tokens=800,)print(completion.result)
pipinstallgoogle-generativeaiimportgoogle.generativeaiasgenaiimportosgenai.configure(api_key=os.environ['API_KEY'])model=genai.GenerativeModel(model_name='gemini-pro')prompt="""You are an expert at solving wordproblems.Solve the following problem:I have three houses, each with threecats. Each cat owns 4 mittens, and a hat.Each mitten was knit from 7m of yarn,each hat from 4m. How much yarn wasneeded to make all the items?Think about it step by step, and showyour work."""completion=model.generate_content(prompt,generation_config={'temperature':0,'max_output_tokens':800})print(completion.text)
Chat : version de base
PaLM
Gemini
pipinstallgoogle-generativeaiimportgoogle.generativeaiaspalmimportospalm.configure(api_key=os.environ['API_KEY'])chat=palm.chat(messages=["Hello."])print(chat.last)# 'Hello! What can I help you with?'chat=chat.reply("Just chillin'")print(chat.last)# 'That's great! ...'
chat.messages[{'author':'0','content':'Hello'},{'author':'1','content':'Hello! How can I help you today?'},{'author':'0','content':"Just chillin'"},{'author':'1','content':"That's great! I'm glad you're able to relax andtakesometimeforyourself.Whatareyouuptotoday?"}]
chat.history[parts{text:"Hello."}role:"user",parts{text:"Greetings! How may I assist you today?"}role:"assistant",parts{text:"Just chillin\'"}role:"user",parts{text:"That\'s great! I\'m glad to hearyou\'re having a relaxing time.MayIofferyouanyvirtualentertainmentorassistance?Icanprovideyouwithmusicrecommendations,playgameswithyou,orengageinafriendlyconversation.\n\nAdditionally,I\'m capable of generatingcreativecontent,suchaspoems,stories,orevensonglyrics.Ifyou\'d like, I can surprise you withsomethingunique.\n\nJustletmeknowwhatyou\'re in the mood for,andI\'ll be happy to oblige."}role:"assistant"]
Chat: Température
PaLM
Gemini
# Setting temperature=1 usually produces more zany responses!chat=palm.chat(messages="What should I eat for dinner tonight? List a few options",temperature=1)chat.last'Here are a few ideas ...
model=genai.GenerativeModel(model_name='gemini-pro')chat=model.start_chat()# Setting temperature=1 usually produces more zany responses!response=chat.send_message("What should I eat for dinner tonight? List a few options",generation_config={'temperature':1.0})print(response.text)'1. Grilled Salmon with Roasted Vegetables: ...'
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2024/08/23 (UTC).
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Il n'y a pas l'information dont j'ai besoin"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Trop compliqué/Trop d'étapes"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Obsolète"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"Problème de traduction"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Mauvais exemple/Erreur de code"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Autre"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Facile à comprendre"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"J'ai pu résoudre mon problème"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Autre"
}]
{"lastModified": "Derni\u00e8re mise \u00e0 jour le 2024/08/23\u00a0(UTC)."}
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2024/08/23 (UTC)."],[],[]]