Spaces:
Build error
Build error
| # test_internlm_api.py | |
| from openai import OpenAI | |
| import os | |
| # 替换为你的 InternLM API Key 和 Base URL,或者设置为环境变量 | |
| api_key = os.getenv('INTERNLM_API_KEY') or "YOUR_API_KEY_HERE" # 建议使用环境变量 | |
| base_url = os.getenv('INTERNLM_API_KEY') or "https://internlm-chat.intern-ai.org.cn/puyu/api/v1/" | |
| model_name = "internlm2.5-latest" | |
| if api_key == "YOUR_API_KEY_HERE": | |
| print("警告:请在脚本中或环境变量中配置您的 InternLM API 密钥。") | |
| client = OpenAI( | |
| api_key=api_key, | |
| base_url=base_url, | |
| ) | |
| try: | |
| chat_rsp = client.chat.completions.create( | |
| model=model_name, | |
| messages=[{"role": "user", "content": "燕知春和江若雪在什么地方认识,她们参加了什么比赛,创立了什么组织?"}], | |
| ) | |
| for choice in chat_rsp.choices: | |
| print(choice.message.content) | |
| except Exception as e: | |
| print(f"API 调用失败: {e}") | |