Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
openai [2025/07/18 21:00] jango |
openai [2025/08/02 19:49] (aktuell) jango |
||
---|---|---|---|
Zeile 2: | Zeile 2: | ||
[[https:// | [[https:// | ||
+ | |||
+ | ====Local==== | ||
+ | <code python> | ||
+ | import requests | ||
+ | import json | ||
+ | import pyttsx3 | ||
+ | |||
+ | # gemma-3-1b-it, | ||
+ | MODEL_NAME = " | ||
+ | API_URL = " | ||
+ | |||
+ | def frage_gpt(prompt_text): | ||
+ | try: | ||
+ | response = requests.post( | ||
+ | API_URL + "/ | ||
+ | headers={" | ||
+ | data=json.dumps({ | ||
+ | " | ||
+ | " | ||
+ | {" | ||
+ | {" | ||
+ | ], | ||
+ | " | ||
+ | }) | ||
+ | ) | ||
+ | if response.status_code == 200: | ||
+ | return response.json() # response.text | ||
+ | else: | ||
+ | return f" | ||
+ | except Exception as e: | ||
+ | return f" | ||
+ | | ||
+ | def frage_gpt_stream(prompt_text): | ||
+ | try: | ||
+ | response = requests.post( | ||
+ | API_URL + "/ | ||
+ | headers={" | ||
+ | data=json.dumps({ | ||
+ | " | ||
+ | " | ||
+ | {" | ||
+ | {" | ||
+ | ], | ||
+ | " | ||
+ | }), | ||
+ | stream=True | ||
+ | ) | ||
+ | |||
+ | if response.status_code != 200: | ||
+ | print(f" | ||
+ | return | ||
+ | | ||
+ | # Antwort-Stream verarbeiten | ||
+ | for line in response.iter_lines(): | ||
+ | if line: | ||
+ | decoded_line = line.decode(" | ||
+ | if decoded_line.startswith(" | ||
+ | payload = json.loads(decoded_line[6: | ||
+ | delta = payload.get(" | ||
+ | if delta: | ||
+ | print(delta, | ||
+ | |||
+ | except Exception as e: | ||
+ | print("" | ||
+ | |||
+ | |||
+ | engine = pyttsx3.init() | ||
+ | engine.setProperty(" | ||
+ | engine.setProperty(" | ||
+ | voices = engine.getProperty(" | ||
+ | for voice in voices: | ||
+ | if " | ||
+ | engine.setProperty(" | ||
+ | break | ||
+ | |||
+ | |||
+ | while True: | ||
+ | frage = input(" | ||
+ | frage_gpt_stream(frage) | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | </ | ||
====Chat Completion==== | ====Chat Completion==== | ||
Zeile 33: | Zeile 116: | ||
print(" | print(" | ||
</ | </ | ||
+ | |||
+ | ====Chat Completion mit Verlauf==== | ||
+ | |||
+ | <code python> | ||
+ | import openai | ||
+ | import os | ||
+ | |||
+ | openai.api_key = os.getenv(" | ||
+ | |||
+ | chat_history = [ | ||
+ | {" | ||
+ | ] | ||
+ | |||
+ | def frage_gpt_mit_verlauf(chat_history): | ||
+ | try: | ||
+ | response = openai.ChatCompletion.create( | ||
+ | model=" | ||
+ | messages=chat_history, | ||
+ | temperature=0.7, | ||
+ | max_tokens=500 | ||
+ | ) | ||
+ | antwort = response[' | ||
+ | return antwort.strip() | ||
+ | except Exception as e: | ||
+ | return f" | ||
+ | |||
+ | def frage_gpt_stream_mit_verlauf(chat_history): | ||
+ | try: | ||
+ | response = openai.ChatCompletion.create( | ||
+ | model=" | ||
+ | messages=chat_history, | ||
+ | temperature=0.7, | ||
+ | max_tokens=500, | ||
+ | stream=True | ||
+ | ) | ||
+ | |||
+ | full_answer = "" | ||
+ | for chunk in response: | ||
+ | if ' | ||
+ | delta = chunk[' | ||
+ | content = delta.get(" | ||
+ | print(content, | ||
+ | full_answer += content | ||
+ | print() | ||
+ | return full_answer.strip() | ||
+ | |||
+ | except Exception as e: | ||
+ | return f" | ||
+ | |||
+ | def main(): | ||
+ | while True: | ||
+ | frage = input(" | ||
+ | if frage.lower() in [' | ||
+ | print(" | ||
+ | break | ||
+ | |||
+ | chat_history.append({" | ||
+ | |||
+ | antwort = frage_gpt_stream_mit_verlauf(chat_history) | ||
+ | #antwort = frage_gpt_mit_verlauf(chat_history) | ||
+ | |||
+ | chat_history.append({" | ||
+ | |||
+ | if __name__ == " | ||
+ | main() | ||
+ | </ | ||
+ | |||
====Function Calling==== | ====Function Calling==== |