-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_prompting_perplexity.py
More file actions
72 lines (64 loc) · 2.97 KB
/
api_prompting_perplexity.py
File metadata and controls
72 lines (64 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# !/usr/bin/env python3
"""
Iteratives Suchanfragen-Skript für Perplexity Sonar API
"""
import os
import pandas as pd
import requests
import json
from api_keys import Perplexity_key
from perplexity import Perplexity
API_URL = "https://api.perplexity.ai/chat/completions"
llm_model = "sonar"
file_path = r"C:\Users\andre\OneDrive\Desktop\Marketing\KI-Performance\KI-Performance Schuhe"
source_file = "KI-Performance Schuhe_2026-01-20" + ".xlsx"
modify_response_filename = "normalize_response.txt"
introduction = "Beantworte zuerst ausschließlich inhaltlich die folgende Frage so, wie du sie auch beantworten würdest, wenn es keine zusätzlichen Format- oder Analyseanforderungen gäbe:"
prompt = "Welche Ballerinas sind bequem für lange Arbeitstage?"
########################################################################################################################
# pip install perplexityai
# Kurz: Über die öffentliche Perplexity‑API kannst du keine fremden Modelle direkt per model="gpt‑5.2" o. Ä. ansteuern;
# du bekommst immer ein Modell aus der Sonar‑Familie, die intern ggf. Drittanbieter nutzt.
# Perplexity nutzt intern Modelle von Anbietern wie OpenAI (GPT‑5.x), Anthropic (Claude), Google (Gemini), xAI (Grok) usw.,
# insbesondere in den UI‑Modi „Pro Search“, „Reasoning“ und „Research“.
def send_prompt(prompt: str) -> str:
headers = {
"Authorization": f"Bearer {Perplexity_key}",
"Content-Type": "application/json",
}
data = {
"model": llm_model,
"messages": [
{"role": "user", "content": prompt}
]
}
resp = requests.post(API_URL, headers=headers, data=json.dumps(data))
resp.raise_for_status() # wirft Fehler bei HTTP-Problem
body = resp.json()
# einfache Text-Ausgabe aus der ersten Choice
return body["choices"][0]["message"]["content"]
def main(row, number_name, prompt_name):
number = row[number_name]
prompt = row[prompt_name]
full_prompt = introduction + "\n" + prompt + "\n" + modify_response
print(f"{number}: {prompt}")
response = send_prompt(full_prompt)
response_final = str(number) + ":" + "\n" + response.replace("\n\n","\n")
return response_final
########################################################################################################################
if __name__ == '__main__':
os.chdir(file_path)
with open(modify_response_filename, "r", encoding="utf-8") as f:
modify_response = f.read()
# Quellendatei mit den 50 Suchanfragen
df_source_file = pd.read_excel(source_file, sheet_name="Suchanfragen")
for n in df_source_file.columns:
if 'Nr' in n:
number_name = n
if 'Such' in n:
prompt_name = n
for ID, row in df_source_file.iterrows():
response = main(row, number_name, prompt_name)
# Speichern der Antworten als Textdatei
with open("full_responses_" + llm_model + "_.txt", "a", encoding="utf-8") as f:
f.write(response + "\n")