Initial commit: 胡汉三千年项目 - 北朝宇宙理论体系
This commit is contained in:
52
ai-tools/scripts/codex_example.py
Normal file
52
ai-tools/scripts/codex_example.py
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
月之暗面 Moonshot AI 使用示例
|
||||
使用月之暗面的 API 进行代码生成
|
||||
"""
|
||||
|
||||
import os
|
||||
from openai import OpenAI
|
||||
|
||||
def main():
|
||||
# 使用月之暗面的 API key 和 base URL
|
||||
api_key = "sk-lEk0pAIZ1EDgUkflq2is5uT2VbhuoKGpO5sNOSnuuccsD68r"
|
||||
base_url = "https://api.moonshot.cn/v1"
|
||||
|
||||
# 初始化客户端
|
||||
client = OpenAI(
|
||||
api_key=api_key,
|
||||
base_url=base_url
|
||||
)
|
||||
|
||||
# 代码生成示例
|
||||
prompt = """
|
||||
# 创建一个 Python 函数来计算斐波那契数列
|
||||
def fibonacci(n):
|
||||
"""
|
||||
|
||||
try:
|
||||
# 月之暗面使用 chat completions API
|
||||
response = client.chat.completions.create(
|
||||
model="moonshot-v1-8k", # 月之暗面的模型
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": "你是一个专业的程序员助手,专门帮助生成高质量的代码。"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"请完成以下 Python 代码:\n{prompt}"
|
||||
}
|
||||
],
|
||||
max_tokens=150,
|
||||
temperature=0.1
|
||||
)
|
||||
|
||||
print("生成的代码:")
|
||||
print(response.choices[0].message.content)
|
||||
|
||||
except Exception as e:
|
||||
print(f"错误: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user