Last active
October 1, 2023 10:38
-
-
Save tiny-rawr/7650792a860aba93b79235677c3e19f4 to your computer and use it in GitHub Desktop.
user_interview_analyser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
import json | |
# Things for you to change: | |
openai.api_key = "YOUR API KEY" | |
interviews = ["INTERVIEW1","INTERVIEW2", "INTERVIEW3", "INTERVIEW4"] | |
# API call to ChatGPT to extract quotes from interviews that are relevant to your questions. | |
def gpt_api_call(model_type, system_behaviour, user_submitted_content, name_of_function, function_description, properties, required_properties): | |
api_call = openai.ChatCompletion.create( | |
model=model_type, | |
messages=[ | |
{"role": "system", "content": system_behaviour}, | |
{"role": "user", "content": user_submitted_content} | |
], | |
functions=[{ | |
"name": name_of_function, | |
"description": function_description, | |
"parameters": { | |
"type": "object", | |
"properties": properties, | |
"required": required_properties | |
} | |
}], | |
function_call={"name": name_of_function} | |
) | |
output = api_call["choices"][0]["message"] | |
data = json.loads(output["function_call"]["arguments"]) if output.get("function_call") else {} | |
data = data.get('interview', []) | |
return data | |
model_type = "gpt-3.5-turbo-16k" | |
system_behaviour = "You analyze interviews with World War II veterans to gain a deeper understanding of their wartime experiences." | |
name_of_function = "analyze_interview" | |
function_description = "You extract direct quotes from interviews that are related to questions asked about the veteran's experiences during World War II. Be comprehensive in your response. Only use direct quotes." | |
properties = { | |
"interview": { | |
"type": "object", | |
"properties": { | |
"Why did they enlist in the military?": { | |
"type": "array", | |
"items": { | |
"type": "string", | |
"description": "A direct quote from the interview that is relevant to the question 'Why did they enlist in the military?'. Use direct quotes only." | |
} | |
}, | |
"Describe their military training": { | |
"type": "array", | |
"items": { | |
"type": "string", | |
"description": "A direct quote from the interview that is relevant to the question 'Describe their military training'. Use direct quotes only." | |
} | |
}, | |
"What were their wartime duties like?": { | |
"type": "array", | |
"items": { | |
"type": "string", | |
"description": "A direct quote from the interview that is relevant to the question 'What were their wartime duties like?'. Use direct quotes only." | |
} | |
}, | |
}, | |
} | |
} | |
required_properties = ["interview"] | |
category_quotes = {} | |
for interview in interviews: | |
response = gpt_api_call(model_type, system_behaviour, interview, name_of_function, function_description, properties, required_properties) | |
for category, quotes in response.items(): | |
if category not in category_quotes: | |
category_quotes[category] = [] | |
for quote in quotes: | |
category_quotes[category].append(quote) | |
for category, quotes in category_quotes.items(): | |
print(f"{category}\n") | |
for i, quote in enumerate(quotes, start=1): | |
print(f"\"{quote}\"") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment