My python code uses API call and data path from where it takes the file and convert it into json index file. Then while making the query it returns the answer from that index file.
I have tried to deploy the on Azure function using VS code but getting error as the response is coming in set but not in str.
Same i have tried in my local machine without azure function , there its working perfectly well.
This is main function from Azure named as init.py, where i have called my python code named as "doc_search"
import logging
import doc_search
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
#name = ', '.join(name)
output = doc_search.query(name)
return func.HttpResponse(output)
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a query string or in the request body for a personalized response.",
status_code=200
)
Second code is doc_search.py
import openai
import os
from langchain import OpenAI
from gpt_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from flask import Flask
from flask_restful import Api
import re
import json
app = Flask(__name__)
api = Api(app)
os.environ["OPENAI_API_KEY"] = 'KEY'
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 600
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-embedding-ada-002", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
index.save_to_disk('index.json')
return index
def chatbot(input_text):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
response = index.query(input_text, response_mode="compact")
return response.response
index = construct_index("doc\docs") #path where the doc stored in azure function folder.
def query(input):
req = chatbot(input)
my_string = list(req)[0].replace("\[","").replace("\]","").replace("\\n","").replace(" ","!DOUBLESPACE!").replace(" ","").replace("!DOUBLESPACE!"," ")
if re.search("is not related to the context information provided.", my_string):
response = openai.Completion.create(
model="text-davinci-003",
prompt=input,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response
elif re.search("not mentioned in the context information provided", my_string):
response = openai.Completion.create(
model="text-davinci-003",
prompt=input,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response
else:
return my_string
if __name__ == "__main__":
app.run()
NOTE: I have also tried adding my python code file name in requirement.txt, still unable to get success.
Kindly help
I used the below code to use OpenAPI, OpenAI key to create an index.json file by reading through documents with doc_search package, load it and call the contents in HTTP trigger like below:-
My Function project :-
My init.py:-
Local function Output:-
Console:-
Browser:-
I deployed the same code in my Azure Function app and the Function URL returned the same output of documents, Refer below:-
Command to deploy HTTP trigger from local machine to Azure:-
Logged into my azure account in the VS code terminal and set my subscription where my Function app exist:-
Command:-
Azure Output:-
I clicked on Azure function URL and got my documents content like below:-
As, An alternative you can use below HTTP function code to use OpenAPI, Open AI without documents directly via query:-
My init.py file:-
My function.json:-
Output:-
Browser:-