forked from Azure-Samples/function-python-ai-openai-chatgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_app.py
52 lines (39 loc) · 1.74 KB
/
function_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import azure.functions as func
import logging
import os
app = func.FunctionApp()
@app.function_name(name='chat')
@app.route(route='chat')
def main(req):
if 'OPENAI_API_KEY' not in os.environ:
raise RuntimeError("No 'OPENAI_API_KEY' env var set. Please see Readme.")
import openai
openai.api_key = os.getenv('OPENAI_API_KEY')
prompt = req.params.get('prompt')
if not prompt:
try:
req_body = req.get_json()
except ValueError:
raise RuntimeError("prompt data must be set in POST.")
else:
prompt = req_body.get('prompt')
if not prompt:
raise RuntimeError("prompt data must be set in POST.")
completion = openai.Completion.create(
model='text-davinci-003',
prompt=generate_prompt(prompt),
temperature=0.9,
max_tokens=200
)
return completion.choices[0].text
def generate_prompt(prompt):
capitalized_prompt = prompt.capitalize()
# prompt template is important to set some context and training up front in addition to user driven input
# Freeform question
return f'{capitalized_prompt}'
# Chat
#return f'The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: {capitalized_prompt}'
# Classification
#return 'The following is a list of companies and the categories they fall into:\n\nApple, Facebook, Fedex\n\nApple\nCategory: '
# Natural language to Python
#return '\"\"\"\n1. Create a list of first names\n2. Create a list of last names\n3. Combine them randomly into a list of 100 full names\n\"\"\"'