import requests import json import os VLAND_API_KEY = os.environ["VLAND_API_KEY"] TEMP_EMPTY_ID = os.environ["TEMP_EMPTY_ID"] TEMP_EMPTY_JSON_ID = os.environ["TEMP_EMPTY_JSON_ID"] class Requester: def __init__(self): self._api_key = VLAND_API_KEY #self.template_ids = template_ids self._url = "https://us-api.vland.live/ai-manage/prompts/{}/chat" def json_output(self, templateID, **kwargs): variables = {"{"+k+"}": v for k, v in kwargs.items()} body = { "variables": variables, "append_messages": [] } headers = {"APIKEY": self._api_key, "Content-Type": "application/json"} res = requests.post(self._url.format(templateID), headers=headers, data=json.dumps(body, ensure_ascii=False).encode("utf-8")) return res.json() def text_output(self, templateID, messages, **kwargs): variables = {"{"+k+"}": v for k, v in kwargs.items()} body = { "variables": variables, "append_messages": messages } headers = {"APIKEY": self._api_key, "Content-Type": "application/json"} print(json.dumps(body, ensure_ascii=False)) res = requests.post(self._url.format(templateID), headers=headers, data=json.dumps(body, ensure_ascii=False).encode("utf-8")) return res.json() if __name__ == '__main__': rqst = Requester() system_content = { "content": "我想让你担任一个情绪低落的员工。你是一位在Audi工作的技术岗位员工,主要负责车载软件的开发和维护工作。最近你因为和爱人分手情绪非常低落,导致工作积极性下降,不能按时赶上deadline。你的manager不知道你的情感问题,但是注意到了你工作质量下降,准备和你来沟通。记住,你的性格非常沉默寡言,不会轻易说出你和爱人分手的事情。除非manager用明确的语言主动询问你的情感经历,你只会犟嘴。你同时还有轻度抑郁的问题。接下来我(用户)将扮演你的manager和你沟通。你的第一句回复应从以下内容选择:\n\t['你好','您好','hihi']" } conv_log = [] while True: user_content = input() conv_log.append({"role":"user", "content":user_content}) res = rqst.text_output(TEMP_EMPTY_ID, conv_log, **system_content) print(res, type(res)) # python requests library doesn't work well with Go-based api functions. especially when it comes to situations where the POST-ing json data has arrays in it.