Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Created February 24, 2024 01:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pamelafox/e6f098bebc0f5464cb6d3b316e500b8f to your computer and use it in GitHub Desktop.
Save pamelafox/e6f098bebc0f5464cb6d3b316e500b8f to your computer and use it in GitHub Desktop.
Using LLM to generate markdown based off GitHub issues
import os
import json
from dotenv import load_dotenv
from openai import AzureOpenAI
import requests
load_dotenv(".env", override=True)
# Use Azure OpenAI to generate a markdown for the issue
openai_client = AzureOpenAI(
api_version="2023-07-01-preview",
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
)
def get_all_issues(repo_name):
issues = requests.get(
f"https://api.github.com/repos/microsoft/{repo_name}/issues"
).json()
issue_numbers = [issue["number"] for issue in issues]
return issue_numbers
def get_issue(issue_num):
# Get the issue from the GitHub API
issue = requests.get(
f"https://api.github.com/repos/microsoft/AI-Chat-App-Hack/issues/{issue_num}"
).json()
# Get the issue body
issue_url = issue["html_url"]
issue_body = issue["body"]
return issue_url, issue_body
def extract_hack_properties(chat_completion):
response_message = chat_completion.choices[0].message
if response_message.tool_calls:
for tool in response_message.tool_calls:
if tool.type != "function":
continue
function = tool.function
if function.name == "describe_hack":
arg = json.loads(function.arguments)
return arg
return {}
def describe_hack(hack_content):
tools = [
{
"type": "function",
"function": {
"name": "describe_hack",
"description": "Describes a hackathon submission based on the provided content.",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name of the hackathon project, based on Project name heading",
},
"one_sentence_description": {
"type": "string",
"description": "One-sentence description of the hackathon project, based on Description heading, but worded in a way that will get people excited to learn more.",
},
"technology_used": {
"type": "string",
"description": "A comma-separated list of Azure services or other technologies used, which may be mentioned in the description - such as Azure AI Search, Azure OpenAI, Azure App Service, etc. If none are mentioned, don't list any.",
},
"video_url": {
"type": "string",
"description": "URL to the project video, based on Project video heading",
},
"repo_url": {
"type": "string",
"description": "URL to the project repository, based on Project Repository URL heading",
}
},
"required": ["search_query"],
},
},
}
]
chat_response = openai_client.chat.completions.create(
# Azure Open AI takes the deployment name as the model name
model=os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME"),
messages=[
{
"role": "system",
"content": "You are a helpful assistant that is preparing copy for a blog post about an exciting hackathon to make AI chat apps using RAG and Azure services. When you describe the hacks, be very supportive and encouraging.",
},
{
"role": "user",
"content": f"Please describe following hackathon submission: {hack_content}",
},
],
tools=tools,
temperature=0.3,
max_tokens=400,
n=1,
stream=False,
)
return extract_hack_properties(chat_response)
def describe_all_issues(repo_name):
hack_descriptions = []
all_issues = get_all_issues(repo_name)
for issue_number in all_issues:
issue_url, issue_body = get_issue(issue_number)
hack_properties = describe_hack(issue_body)
hack_properties["submission_url"] = issue_url
print(hack_properties)
hack_descriptions.append(hack_properties)
# write to file
with open("hack_descriptions.json", "w") as f:
json.dump(hack_descriptions, f, indent=2)
def generate_markdown():
markdown_str = ""
with open("hack_descriptions.json", "r") as f:
hack_descriptions = json.load(f)
markdown_str += "# Hackathon Submissions\n"
for hack in hack_descriptions:
markdown_str += f"## {hack['name']}\n"
markdown_str += f"{hack['one_sentence_description']}\n\n"
markdown_str += f"Technology used: {hack['technology_used']}\n\n"
markdown_str += f"See: [Video]({hack['video_url']}) | [Repo]({hack['repo_url']}) | [Full Submission]({hack['submission_url']})\n\n"
# save to file
with open("hackathon_blog.md", "w") as f:
f.write(markdown_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment