import yaml import requests from pathlib import Path from tools.utils import get_config class FastGPTClient: def __init__(self): self.config = get_config() self.headers = { "Content-Type": "application/json", "Authorization": f"Bearer {self.config.get('fastgpt.key')}" } def chat(self, prompt: str, temperature: float = 0.7) -> str: """与FastGPT进行对话""" payload = { "chatId": "my_chatId111", "stream": False, "detail": False, "messages": [ { "role": "user", "content": prompt } ] } try: response = requests.post( self.config.get('fastgpt.url'), headers=self.headers, json=payload, timeout=500 ) response.raise_for_status() data = response.json() return data['choices'][0]['message']['content'] except Exception as e: print(f"API调用失败: {str(e)}") raise e if __name__ == "__main__": try: client = FastGPTClient() response = client.chat("总结一下知识库总定额编号相关的内容") print("FastGPT回复:", response) except Exception as e: print(f"发生异常: {str(e)}")