I’m looking for a few samples of JSON messages for the API to translate text from language A to language B.
- for a string
- for a textfile (not a URL)
Regards, Gerrit
I’m looking for a few samples of JSON messages for the API to translate text from language A to language B.
Regards, Gerrit
I think nobody has responded, because nobody can understand your request.
Are you wanting to see examples of the HTTP body to send to one of the OpenAI models in order to ask for a translation?
Do you want examples of JSON that have contents that should be translated by an AI?
I asked two different AI models to produce what you are requesting, and they gave me output that is probably not what you desire:
{
“q”: “Your text to translate”,
“source”: “language_A”,
“target”: “language_B”,
“format”: “text”
}
You don’t need to specify your source language A. When the text file or string is submitted to the API, the systems just tokenizes the words. Just specify the target language B with the instruction to translate.
prompt
[
{
“role”: “system”,
“content”: “translate to japanese.”
},
{
"role": "user",
"content": "Placeholder for context and content"
}
]
You can just write a json file to read in as prompt, then stick in context and submit to api.
def read_text_file(txt_path):
with open(txt_path, ‘r’, encoding=‘utf-8’) as file:
return file.read()
prompt_text = prompt_entry.get()
with open(prompt_text, "r") as f:
messages = json.load(f)
context_file = context_entry.get()
context_text = read_text_file(context_file)
for message in messages:
if message["role"] == "user":
message["content"] = f" {context_text} "
response = openai.chat.completions.create(
model=MODEL,
messages=messages,
temperature=TEMPERATURE,
max_tokens=4000,
)
I understand what this should do, but how do I add a “placeholder” to a local .txt file. How do I make a proper reference to this txt file?
I can communicate through the API but GPT keeps asking for the text to translate
Regards, Gerrit
here is a snippet , i just use a quick window in tkinter for me. You can do whatever your want to inputs. so replace all the .get
If you just have a string and want that as context, the same thing works. You really don’t care how long the text is as long as you are under model limit.
import tkinter as tk
from tkinter import ttk
import json
def read_text_file(txt_path):
with open(txt_path, 'r', encoding='utf-8') as file:
return file.read()
prompt_text = prompt_entry.get()
# this is the file containing your json
# you read the file and load it as a json
with open(prompt_text, "r") as f:
messages = json.load(f)
context_file = context_entry.get()
context_text = read_text_file(context_file)
#you read in your conext textfile
#you adjust your json messages by replacing the placeholder with context_text
for message in messages:
if message["role"] == "user":
message["content"] = f" {context_text} "
response = openai.chat.completions.create(
model=MODEL,
messages=messages,
temperature=TEMPERATURE,
max_tokens=4000,
)
print(json.dumps(json.loads(response.model_dump_json()), indent=4))
#dump your response json to see what is going on for token usage etc
Or: if the question was actually sending a JSON string that would translate:
import os
import httpx
import json
import brotli
import asyncio
url = "https://api.openai.com/v1/chat/completions"
headers = {
"User-Agent": "JSON Buddy",
"Accept": "application/json",
"Content-Type": "application/json",
"Accept-Encoding": "br",
"Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}"
}
translate = "The big bug bit the bear."
json_string = f'''
{{
"model": "gpt-3.5-turbo", "top_p": 0.2,
"messages": [
{{
"role": "system",
"content": "You are a helpful language translator."
}},
{{
"role": "user",
"content": "Translate to Spanish:\\n{translate}"
}}
]
}}
'''
body = ' '.join(json_string.strip().split())
async def fetch_response():
async with httpx.AsyncClient(http2=True) as client:
response = await client.post(url, headers=headers, content=body)
if response.status_code == 200:
content = json.loads(response.content)['choices'][0]['message']['content']
print(content)
else:
print(f"API ERROR: {response.status_code}: {response.content}")
if __name__ == "__main__":
asyncio.run(fetch_response())