How to deploy python code to azure function using vs code

510 Views Asked by At

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

1

There are 1 best solutions below

0
On

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 :-

enter image description here

My init.py:-

import os

import json

import azure.functions as func

  

def  generate_index_json():

# Get the absolute path of the 'doc' directory

doc_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "doc")

  

# Get a list of all .txt files in the 'doc' directory

txt_files = [f  for  f  in os.listdir(doc_dir) if  f.endswith(".txt")]

  

# Read the contents of each file and append to a list

documents = []

for  txt_file  in  txt_files:

with  open(os.path.join(doc_dir, txt_file), "r") as  f:

document = f.read()

documents.append(document)

  

# Write the list of documents to the index.json file

with  open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.json"), "w") as  f:

json.dump(documents, f)

  

def  main(req: func.HttpRequest) -> func.HttpResponse:

# Get the absolute path of the index.json file

index_json_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.json")

  

# Check if index.json file exists

if  not os.path.exists(index_json_path):

generate_index_json()

  

# Load documents from index.json file

with  open(index_json_path, "r") as  f:

documents = json.load(f)

  

# Convert documents to string

document_string = "\n".join(documents)

  

# Return documents in HTTP response

return func.HttpResponse(document_string)

Local function Output:-

Console:-

enter image description here

Browser:-

enter image description here

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:-

az login az account set --subscription "<subscription-name>"
func azure functionapp publish azure-function-app-name

Azure Output:-

enter image description here

I clicked on Azure function URL and got my documents content like below:-

enter image description here

As, An alternative you can use below HTTP function code to use OpenAPI, Open AI without documents directly via query:-

My init.py file:-

import os

import json

import openai

import azure.functions as func

  

# Set up OpenAI API key

openai.api_key = ("<api-key>")

  

# Define function to generate index.json file

def  generate_index_json():

# Define search query

query = "example query"

  

# Define search model

model = "model id"

  

# Define search parameters

search_params = {

"documents": [],

"query": query,

"model": model

}

  

# Retrieve search results

search_results = openai.Completion.create(

engine="davinci",

prompt=json.dumps(search_params),

max_tokens=1024,

n=1,

stop=None,

temperature=0.5,

)

  

# Extract documents from search results

documents = search_results.choices[0].text.strip().split("\n")

  

# Write documents to index.json file

with  open("index.json", "w") as  f:

json.dump(documents, f)

  

def  main(req: func.HttpRequest) -> func.HttpResponse:

# Check if index.json file exists

if  not os.path.exists("index.json"):

generate_index_json()

  

# Load documents from index.json file

with  open("index.json", "r") as  f:

documents = json.load(f)

  

# Convert documents to string

document_string = "\n".join(documents)

  

# Return documents in HTTP response

return func.HttpResponse(document_string)

My function.json:-

{

"scriptFile": "__init__.py",

"entryPoint": "main",

"bindings": [

{

"authLevel": "anonymous",

"type": "httpTrigger",

"direction": "in",

"name": "req",

"methods": [

"get"

]

},

{

"type": "http",

"direction": "out",

"name": "$return"

}

]

}

Output:-

enter image description here

Browser:-

enter image description here