After the hint of the other answer, I got it done with subprocess.Popen()
, see the code at GenAI How can I open a licensed GPT chat like on https://chat.openai.com/ on my own user if I have an API key that is not from my user?, search for the heading: "Run the curl string in the CLI and get its output in Python" at the end:
Run the curl string in the CLI and get its output in Python
With this string at hand, you can run curl in the CLI and still get
its output in Python, taken from Json parsing Python
subprocess:
import subprocess
import json
def getProcessOutput(cmd):
process = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE)
process.wait()
data, err = process.communicate()
if process.returncode is 0:
return data.decode('utf-8')
else:
print("Error:", err)
return ""
# for domain in getProcessOutput("cat /etc/localdomains").splitlines():
cmd = str_curl
data = getProcessOutput(cmd)
json.loads(data)
Out:
{'id': 'chatcmpl-abc123......................',
'object': 'chat.completion', 'created': 1706123456, 'model':
'gpt-4-0613', 'choices': [{'index': 0, 'message': {'role':
'assistant',
'content': '"arn:aws:sagemaker:us-west-2:123456789012:model-package/mymodelpackage"'},
'logprobs': None, 'finish_reason': 'stop'}], 'usage':
{'prompt_tokens': 32, 'completion_tokens': 24, 'total_tokens': 56},
'system_fingerprint': None}
With that done and loaded into a json dictionary, you can check the
mere answer in the json tree with:
print(json.loads(data)['choices'][0]['message']['content'])
Out:
"arn:aws:sagemaker:us-west-2:123456789012:model-package/mymodelpackage"
The main clue of subprocess.Popen()
comes from Stack Overflow Json parsing Python subprocess and another answer there that shows that you can shorten it to process.communicate()[0]
.
a_dict = json.loads(a_string)
... maybe (probably) – Bergmann