-
-
Save gangtao/b8c6bb1f5cc096f226a2f3fb2380f264 to your computer and use it in GitHub Desktop.
Timeplus Python UDF - OpenAI Chat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION chat(value string) RETURNS string LANGUAGE PYTHON AS | |
$$ | |
import os | |
from openai import OpenAI | |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
def chat(value): | |
res = [] | |
for v in value: | |
try: | |
chat_completion = client.chat.completions.create( | |
messages=[{ | |
"role": "user", | |
"content": v | |
}], | |
model="gpt-3.5-turbo") | |
res.append(chat_completion.choices[0].message.content) | |
except Exception as e: | |
res.append(str(e)) | |
return res | |
$$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment