百度智能云

import requests API_KEY = 'DqubskmEPyRGAfJDK0zPNFN5' SECRET_KEY = 'GP55DeZVt0V6HbabD6DVCAAe5AlRGPLC' TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token' API_URL = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k' def get_baidu_access_token(api_key, secret_key): params = { 'grant_type': 'client_credentials', 'client_id': api_key, 'client_secret': secret_key } response = requests.get(TOKEN_URL, params=params) if response.status_code == 200: token_info = response.json() return token_info.get('access_token') else: raise Exception("Failed to get access token from Baidu. Status code: {}".format(response.status_code)) access_token = get_baidu_access_token(API_KEY, SECRET_KEY) def get_learning_path(component): user_message = { "role": "user", "content": f"请用中文提供关于使用 {component} 的学习路径。包括基本信息,推荐的开发板,官方文档,教程,项目,社区支持,常见问题解答和高级阅读资料。" } headers = { 'Content-Type': 'application/json' } payload = { 'messages': [user_message], 'stream': False } response = requests.post(f"{API_URL}?access_token={access_token}", headers=headers, json=payload) api_response = response.json() if response.status_code == 200 and 'result' in api_response: return api_response['result'] else: error_message = api_response.get('error_msg', 'Invalid response from API') raise Exception(f"API Error: {error_message}")