Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
|
openai [2025/11/12 22:16] jango angelegt |
openai [2025/12/18 21:40] (aktuell) jango |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | =====API===== | ||
| + | [[https:// | ||
| + | |||
| + | Kosten pro Model: [[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==== | ||
| + | |||
| + | <code python> | ||
| + | import openai | ||
| + | import os | ||
| + | |||
| + | # API-Key einfügen oder aus Umgebungsvariable lesen | ||
| + | openai.api_key = os.getenv(" | ||
| + | |||
| + | def frage_gpt(prompt_text): | ||
| + | try: | ||
| + | response = openai.ChatCompletion.create( | ||
| + | model=" | ||
| + | messages=[ | ||
| + | {" | ||
| + | {" | ||
| + | ], | ||
| + | temperature=0.7, | ||
| + | max_tokens=500 | ||
| + | ) | ||
| + | antwort = response[' | ||
| + | return antwort.strip() | ||
| + | except Exception as e: | ||
| + | return f" | ||
| + | |||
| + | # Beispiel | ||
| + | frage = input(" | ||
| + | antwort = frage_gpt(frage) | ||
| + | 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==== | ||
| + | |||
| + | <code python> | ||
| + | import openai | ||
| + | import os | ||
| + | import json | ||
| + | import requests | ||
| + | |||
| + | # API-Key einfügen oder aus Umgebungsvariable lesen | ||
| + | openai.api_key = os.getenv(" | ||
| + | |||
| + | tools = [ | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | }, | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | }, | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | }, | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | ] | ||
| + | |||
| + | messages = [{" | ||
| + | response = openai.ChatCompletion.create( | ||
| + | model=" | ||
| + | messages=messages, | ||
| + | tools=tools, | ||
| + | tool_choice=" | ||
| + | ) | ||
| + | |||
| + | response_message = response[' | ||
| + | tool_calls = response_message.get(" | ||
| + | |||
| + | def get_weather(location): | ||
| + | return f"The current temperature in {location} is 23 degrees and sunny." | ||
| + | |||
| + | def list_files(directory_path): | ||
| + | try: | ||
| + | files = os.listdir(directory_path) | ||
| + | return [f for f in files if os.path.isfile(os.path.join(directory_path, | ||
| + | except Exception as e: | ||
| + | return f" | ||
| + | |||
| + | if tool_calls: | ||
| + | for call in tool_calls: | ||
| + | function_name = call[' | ||
| + | arguments = json.loads(call[' | ||
| + | | ||
| + | if function_name == " | ||
| + | location = arguments[' | ||
| + | result = list_files(location) | ||
| + | messages.append(response_message) | ||
| + | messages.append({ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | followup = openai.ChatCompletion.create( | ||
| + | model=" | ||
| + | messages=messages | ||
| + | ) | ||
| + | print(followup[' | ||
| + | |||
| + | # Funktion ausführen | ||
| + | if function_name == " | ||
| + | location = arguments[' | ||
| + | result = get_weather(location) | ||
| + | messages.append(response_message) | ||
| + | messages.append({ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | followup = openai.ChatCompletion.create( | ||
| + | model=" | ||
| + | messages=messages | ||
| + | ) | ||
| + | print(followup[' | ||
| + | | ||
| + | if function_name == " | ||
| + | query = arguments[' | ||
| + | result = search_web(query) | ||
| + | messages.append(response_message) | ||
| + | messages.append({ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | followup = openai.ChatCompletion.create( | ||
| + | model=" | ||
| + | messages=messages | ||
| + | ) | ||
| + | print(followup[' | ||
| + | |||
| + | else: | ||
| + | # Kein Tool wurde verwendet | ||
| + | print(response_message[' | ||
| + | </ | ||
| + | |||
| + | ====Chat History mit Tools==== | ||
| + | |||
| + | <code python> | ||
| + | import openai | ||
| + | import os | ||
| + | import json | ||
| + | |||
| + | # API-Key setzen | ||
| + | openai.api_key = os.getenv(" | ||
| + | |||
| + | # Tool-Definitionen | ||
| + | tools = [ | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | }, | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | }, | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | }, | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | }, | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | }, | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | ] | ||
| + | |||
| + | # Tool-Funktionen | ||
| + | def get_weather(location): | ||
| + | return f"The current temperature in {location} is 23 degrees and sunny." | ||
| + | |||
| + | def list_files(directory_path): | ||
| + | try: | ||
| + | files = os.listdir(directory_path) | ||
| + | filelist = [f for f in files if os.path.isfile(os.path.join(directory_path, | ||
| + | folders = [f for f in os.listdir(directory_path) if os.path.isdir(os.path.join(directory_path, | ||
| + | return filelist + folders | ||
| + | | ||
| + | except Exception as e: | ||
| + | return f" | ||
| + | |||
| + | def search_web(query): | ||
| + | return f" | ||
| + | |||
| + | def read_file(path): | ||
| + | try: | ||
| + | with open(path, ' | ||
| + | content = f.read() | ||
| + | return content | ||
| + | except Exception as e: | ||
| + | return f" | ||
| + | |||
| + | |||
| + | # GPT-Interaktion mit Tool-Unterstützung | ||
| + | def frage_gpt_mit_tools(chat_history): | ||
| + | try: | ||
| + | response = openai.ChatCompletion.create( | ||
| + | model=" | ||
| + | messages=chat_history, | ||
| + | tools=tools, | ||
| + | tool_choice=" | ||
| + | ) | ||
| + | |||
| + | response_message = response[' | ||
| + | tool_calls = response_message.get(" | ||
| + | |||
| + | if tool_calls: | ||
| + | for call in tool_calls: | ||
| + | function_name = call[' | ||
| + | arguments = json.loads(call[' | ||
| + | |||
| + | if function_name == " | ||
| + | result = list_files(arguments[' | ||
| + | |||
| + | elif function_name == " | ||
| + | result = get_weather(arguments[' | ||
| + | |||
| + | elif function_name == " | ||
| + | result = search_web(arguments[' | ||
| + | | ||
| + | elif function_name == " | ||
| + | result = read_file(arguments[' | ||
| + | |||
| + | chat_history.append(response_message) | ||
| + | chat_history.append({ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }) | ||
| + | |||
| + | followup = openai.ChatCompletion.create( | ||
| + | model=" | ||
| + | messages=chat_history | ||
| + | ) | ||
| + | antwort = followup[' | ||
| + | else: | ||
| + | antwort = response_message[' | ||
| + | |||
| + | return antwort.strip() | ||
| + | |||
| + | except Exception as e: | ||
| + | return f" | ||
| + | |||
| + | # Hauptfunktion | ||
| + | def main(): | ||
| + | chat_history = [ | ||
| + | {" | ||
| + | ] | ||
| + | |||
| + | while True: | ||
| + | frage = input("########## | ||
| + | if frage.lower() in [' | ||
| + | print(" | ||
| + | break | ||
| + | |||
| + | chat_history.append({" | ||
| + | antwort = frage_gpt_mit_tools(chat_history) | ||
| + | print(antwort) | ||
| + | chat_history.append({" | ||
| + | |||
| + | if __name__ == " | ||
| + | main() | ||
| + | </ | ||