########################################################################
# input structure : pyInputData = { "contentType": contentType, "textList": ["A", "B"] }
# contentType : "text/plain"
# output structure : pyOutData = { "contentType": contentType, "textList": ["C", "D"] }
#########################################################################
from openai import OpenAI # Import the OpenAI package
import os # Import the os package
API_KEY="<key>"
API_URL="https://api.perplexity.ai"
def UserFunction(param1):
#------- Your code starts here -------
textList = ["HELLO", "WORLD" ]
print(str(param1))
if param1:
textList = param1.get("textList")
client = OpenAI(base_url=API_URL, api_key=API_KEY)
completion = client.chat.completions.create(
model="sonar",
messages=[
{"role": "user", "content": textList[0]}
]
)
if param1:
textList = param1.get("textList")
textList.append(completion.choices[0].message.content)
out={ "contentType": "text/plain", "textList": textList }
#------- Your code ends here -------
return out
# pyOutData is a global variable already defined by caller of this code.
# Following two lines of code should not be touched
global pyOutData, pyInputData
pyOutData = UserFunction(pyInputData)