Files
playground/openai_example_sentences.py
2024-12-24 20:49:41 +03:00

58 lines
1.3 KiB
Python

import argparse
import httpx
OPENAPI_TOKEN = "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"
http = httpx.Client(
base_url='https://api.openai.com/v1/',
headers={
'Authorization': f'Bearer {OPENAPI_TOKEN}',
},
timeout=30,
)
def generate_examples(text: str) -> str:
payload = {
"messages": [
{
"role": "system",
"content": """
You are to provide colloquial and idiomatic German translations of given text. List 10 alternatives.
""",
},
{
"role": "user",
"content": text,
},
],
"temperature": 0.7,
"max_tokens": 256,
"top_p": 1,
"frequency_penalty": 1.06,
"presence_penalty": 0.42,
"model": "gpt-3.5-turbo",
"stream": False,
}
res = http.post('/chat/completions', json=payload)
res.raise_for_status()
return res.json()['choices'][0]['message']['content'].strip()
def parse_args():
arger = argparse.ArgumentParser()
arger.add_argument('prompt', help='Phrase to generate sentences for')
return arger.parse_args()
def main():
args = parse_args()
response = generate_examples(args.prompt)
print(response)
if __name__ == '__main__':
main()