Skip to content

Commit 8f419b4

Browse files
authored
Update generate_docs.py
1 parent bf99c2d commit 8f419b4

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

scripts/generate_docs.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
import cohere
2+
import requests
33

44
# -----------------------------
55
# Path Setup (Local + GitHub CI)
@@ -15,13 +15,17 @@
1515
os.makedirs(OUTPUT_DIR, exist_ok=True)
1616

1717
# -----------------------------
18-
# Cohere Chat API Setup
18+
# OpenRouter API Setup
1919
# -----------------------------
20-
COHERE_API_KEY = os.getenv("COHERE_API_KEY")
21-
if not COHERE_API_KEY:
22-
raise RuntimeError("COHERE_API_KEY environment variable not set")
20+
API_KEY = os.getenv("OPENROUTER_API_KEY")
21+
if not API_KEY:
22+
raise RuntimeError("OPENROUTER_API_KEY environment variable not set")
2323

24-
co = cohere.Client(COHERE_API_KEY)
24+
URL = "https://openrouter.ai/api/v1/chat/completions"
25+
HEADERS = {
26+
"Authorization": f"Bearer {API_KEY}",
27+
"Content-Type": "application/json"
28+
}
2529

2630
# -----------------------------
2731
# Functions
@@ -39,27 +43,29 @@ def read_ansible_role(role_path):
3943
return data
4044

4145
def generate_docs(content, role_name):
42-
"""Generate Markdown documentation using Cohere Chat API"""
43-
messages = [
44-
{
45-
"role": "system",
46-
"content": "You are a helpful assistant that generates documentation for Ansible roles in Markdown format."
47-
},
48-
{
49-
"role": "user",
50-
"content": f"Document the Ansible role '{role_name}'. Include:\n- Purpose of the role\n- Main tasks\n- Variables and defaults\n- Dependencies or role requirements\n\nYAML content:\n{content}"
51-
}
52-
]
46+
"""Generate Markdown documentation using OpenRouter Chat API"""
47+
prompt = f"""
48+
Document the Ansible role '{role_name}'. Include:
49+
- Purpose of the role
50+
- Main tasks
51+
- Variables and defaults
52+
- Dependencies or role requirements
5353
54-
response = co.chat(
55-
model="command-a-03-2025", # Free-tier compatible chat model
56-
messages=messages,
57-
max_tokens=1000,
58-
temperature=0.5
59-
)
54+
YAML content:
55+
{content}
56+
"""
6057

61-
# Access the generated text
62-
return response.message.content[0].text.strip()
58+
payload = {
59+
"model": "gpt-4o-mini", # free-tier compatible model
60+
"messages": [{"role": "user", "content": prompt}],
61+
"temperature": 0.5,
62+
"max_tokens": 1000
63+
}
64+
65+
response = requests.post(URL, headers=HEADERS, json=payload)
66+
response.raise_for_status()
67+
result = response.json()
68+
return result["choices"][0]["message"]["content"].strip()
6369

6470
# -----------------------------
6571
# Main Script

0 commit comments

Comments
 (0)