79110066

Date: 2024-10-21 12:38:49
Score: 2.5
Natty:
Report link

You need to create the Azure OpenAI client by using AsyncAzureOpenAI as given below.

client = openai.AsyncAzureOpenAI(
    api_key = os.getenv("GPT_AZURE_OPENAI_API_KEY"),  
    api_version = "2024-08-01-preview",
    azure_endpoint = os.getenv("GPT_AZURE_OPENAI_ENDPOINT")
    )

Your complete code should look like below.

import azure.functions as func
import logging
from langchain_openai import AzureOpenAIEmbeddings
import os
import openai
from langchain_community.vectorstores.azuresearch import AzureSearch
import json
from azurefunctions.extensions.http.fastapi import Request, StreamingResponse
import asyncio

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

async def stream_processor(response):
    async for chunk in response:
        if len(chunk.choices) > 0:
            delta = chunk.choices[0].delta
            if delta.content: 
                await asyncio.sleep(0.1)
                yield delta.content


@app.route(route="http_trigger", methods=[func.HttpMethod.POST])
async def http_trigger(req: Request) -> StreamingResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    req_body = await req.body()  
    logging.info(f'Request body: {req_body}') 
    data = json.loads(req_body.decode('utf-8'))
    variable1 = data.get("query")
    
    embeddings: AzureOpenAIEmbeddings = AzureOpenAIEmbeddings(
        azure_deployment=os.environ.get("EMB_AZURE_DEPLOYMENT"),
        openai_api_version=os.environ.get("EMB_AZURE_OPENAI_API_VERSION"),
        azure_endpoint=os.environ.get("EMB_AZURE_ENDPOINT"),
        api_key=os.environ.get('EMB_AZURE_OPENAI_API_KEY'),
    )

    index_name= "de-iceing-test"
    vector_store: AzureSearch = AzureSearch(
        azure_search_endpoint=os.environ.get("VECTOR_STORE_ADDRESS"),
        azure_search_key=os.environ.get("VECTOR_STORE_PASSWORD"),
        index_name=index_name,
        embedding_function=embeddings.embed_query,
    )

    client = openai.AsyncAzureOpenAI(
    api_key = os.getenv("GPT_AZURE_OPENAI_API_KEY"),  
    api_version = "2024-08-01-preview",
    azure_endpoint = os.getenv("GPT_AZURE_OPENAI_ENDPOINT")
    )

    def userchat(query):
        docs = vector_store.similarity_search(
            query=fr"{query}",
            k=2,
            search_type="similarity",
        )
        chunk = [item.page_content for item in docs]
        return chunk
    
    context = userchat(variable1)
    prompt = fr"Content: {context} \
                please provide a clear and concise answer to the user's query/question mentioned below.\
                Query: {variable1}.\
                If the query is about the procedue or the method then generate the text in bullet points."
    prompt1 = fr"Here is the query from the user, Query: {variable1} \
                Can you please provide a clear and concise solution to the user's query based on the content below and summary below.\
                Content: {context}"
    
    logging.info(prompt)

    response = await client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "user", "content": f"{prompt}"}
        ],
        stream=True
    )
    logging.info(f"OpenAI API Response Object: {response}")

    if response is not None:
        logging.info("Streaming started...")
        return StreamingResponse(stream_processor(response), media_type="text/event-stream")
    else:
        return func.HttpResponse(
            "Error: Response is not iterable or streaming not supported.",
            status_code=500
    )

enter image description here

Reasons:
  • RegEx Blacklisted phrase (2.5): please provide
  • RegEx Blacklisted phrase (2.5): Can you please provide
  • Long answer (-1):
  • Has code block (-0.5):
  • High reputation (-1):
Posted by: Ikhtesam Afrin