Initial commit: 胡汉三千年项目 - 北朝宇宙理论体系
This commit is contained in:
59
ai-tools/scripts/codex_chat_example.py
Normal file
59
ai-tools/scripts/codex_chat_example.py
Normal file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
使用月之暗面 Moonshot AI 进行代码生成
|
||||
月之暗面提供强大的中文代码生成能力
|
||||
"""
|
||||
|
||||
import os
|
||||
from openai import OpenAI
|
||||
|
||||
def generate_code(client, description):
|
||||
"""使用月之暗面 API 生成代码"""
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model="moonshot-v1-8k", # 月之暗面模型
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": "你是一个专业的程序员助手,专门帮助生成高质量的代码。"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"请生成以下功能的 Python 代码:{description}"
|
||||
}
|
||||
],
|
||||
max_tokens=500,
|
||||
temperature=0.1
|
||||
)
|
||||
|
||||
return response.choices[0].message.content
|
||||
|
||||
except Exception as e:
|
||||
return f"错误: {e}"
|
||||
|
||||
def main():
|
||||
# 使用月之暗面的配置
|
||||
api_key = "sk-lEk0pAIZ1EDgUkflq2is5uT2VbhuoKGpO5sNOSnuuccsD68r"
|
||||
base_url = "https://api.moonshot.cn/v1"
|
||||
|
||||
# 初始化客户端
|
||||
client = OpenAI(
|
||||
api_key=api_key,
|
||||
base_url=base_url
|
||||
)
|
||||
|
||||
# 示例:生成不同类型的代码
|
||||
examples = [
|
||||
"一个计算两个数字最大公约数的函数",
|
||||
"一个简单的 HTTP 服务器类",
|
||||
"一个数据验证装饰器"
|
||||
]
|
||||
|
||||
for i, description in enumerate(examples, 1):
|
||||
print(f"\n=== 示例 {i}: {description} ===")
|
||||
code = generate_code(client, description)
|
||||
print(code)
|
||||
print("-" * 50)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user