Lavorare con l'API di OpenAI
James Chapman
Curriculum Manager, DataCamp
response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "system", "content": "Sei un tutor di programmazione Python che parla in modo conciso."},{"role": "user", "content": "Come si definisce una lista in Python?"},{"role": "assistant", "content": "Le liste si definiscono racchiudendo una sequenza di oggetti separati da virgole tra parentesi quadre [ ]."},{"role": "user", "content": "Qual è la differenza tra oggetti mutabili e immutabili?"}] )





messages = [{"role": "system", "content": "Sei un tutor di data science che fornisce spiegazioni brevi e semplici."}]user_qs = ["Perché Python è così popolare?", "Riassumi questo in una frase."]for q in user_qs:print("Utente: ", q)user_dict = {"role": "user", "content": q}messages.append(user_dict)response = client.chat.completions.create( model="gpt-4o-mini", messages=messages )assistant_dict = {"role": "assistant", "content": response.choices[0].message.content} messages.append(assistant_dict)print("Assistente: ", response.choices[0].message.content, "\n")
User: Why is Python so popular?
Assistant: Python is popular for many reasons, including its simplicity,
versatility, and wide range of available libraries. It has a relatively
easy-to-learn syntax that makes it accessible to beginners and experts alike. It
can be used for a variety of tasks, such as data analysis, web development,
scientific computing, and machine learning. Additionally, Python has an active
community of developers who contribute to its development and share their
knowledge through online resources and forums.
User: Summarize this in one sentence.
Assistant: Python is popular due to its simplicity, versatility, wide range of
libraries, and active community of developers.
Lavorare con l'API di OpenAI