Skip to content
Snippets Groups Projects

umwandlung _von_text_in_json_object

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by chaudharyto95333

    Umwandlungen von Texteingaben in ein JSON-Objekt.

    Edited
    .env 54 B
    app.py 748 B
    from dotenv import load_dotenv
    from openai import OpenAI
    import os
    import json
    
    load_dotenv()
    
    # Rufe den API-Key sicher ab
    api_key = os.getenv("OPENAI_API_KEY")
    if not api_key:
        raise ValueError("API Key ist nicht gesetzt. Pruefe deine .env Datei oder die Umgebungsvariablen.")
    
    client = OpenAI(api_key=api_key)
    
    response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        response_format={"type": "json_object"},
        messages=[{"role": "system",
                   "content": "Wandle die Abfrage des Benutzers in ein JSON-Objekt um"},
                  {"role": "user",
                   "content": "Ich suche nach Bücher von Mark Twain aus den Erscheinungsjahren zwischen 1875 und 1880"}])
    
    print(response.choices[0].message.content)
    requirements.txt 263 B
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment