Skip to content

Instantly share code, notes, and snippets.

@mrtngrsbch
Forked from mouredev/chatgpt_api.py
Created March 25, 2023 08:06
Show Gist options
  • Save mrtngrsbch/78a8bc50f09234cb45e6a8d6ec1416ec to your computer and use it in GitHub Desktop.
Save mrtngrsbch/78a8bc50f09234cb45e6a8d6ec1416ec to your computer and use it in GitHub Desktop.
Ejemplo de uso del API de ChatGPT desde Python
import openai # pip install openai
import typer # pip install "typer[all]"
from rich import print # pip install rich
from rich.table import Table
"""
Webs de interés:
- Módulo OpenAI: https://github.com/openai/openai-python
- Documentación API ChatGPT: https://platform.openai.com/docs/api-reference/chat
- Typer: https://typer.tiangolo.com
- Rich: https://rich.readthedocs.io/en/stable/
"""
def main():
openai.api_key = "TU_API_KEY creada en https://platform.openai.com"
print("💬 [bold green]ChatGPT API en Python[/bold green]")
table = Table("Comando", "Descripción")
table.add_row("exit", "Salir de la aplicación")
table.add_row("new", "Crear una nueva conversación")
print(table)
# Contexto del asistente
context = {"role": "system",
"content": "Eres un asistente muy útil."}
messages = [context]
while True:
content = __prompt()
if content == "new":
print("🆕 Nueva conversación creada")
messages = [context]
content = __prompt()
messages.append({"role": "user", "content": content})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages)
response_content = response.choices[0].message.content
messages.append({"role": "assistant", "content": response_content})
print(f"[bold green]> [/bold green] [green]{response_content}[/green]")
def __prompt() -> str:
prompt = typer.prompt("\n¿Sobre qué quieres hablar? ")
if prompt == "exit":
exit = typer.confirm("✋ ¿Estás seguro?")
if exit:
print("👋 ¡Hasta luego!")
raise typer.Abort()
return __prompt()
return prompt
if __name__ == "__main__":
typer.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment