Giter Club home page Giter Club logo

Comments (2)

biancat821 avatar biancat821 commented on June 2, 2024

I managed to solve this issue by adding a new method called get_token in the "evaluate" function that retreive a token that allows the access to the chat, with login enabled.

To use this new method you need:

  • tenant_id: str,
  • client_id: str,
  • client_secret: str,

For client_id and client_secret check
https://learn.microsoft.com/en-us/answers/questions/834401/hi-i-want-my-client-id-and-client-secret-key

The client_secret key is visible only when it is created therefore it is likely that you need to add a new one, check
https://techcommunity.microsoft.com/t5/microsoft-defender-for-identity/app-secret-application-secret-azure-ad-azure-ad-app-secrets/m-p/3775325

The get_token method to retrieve the token

def get_token(tenant_id:str, client_id: str, client_secret: str):
    
    token_url = "https://login.microsoftonline.com/{tenant_id}/oauth2/token"

    data = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret,
        'resource': "api://" + client_id
    }

    # Make a POST request to the token endpoint
    response = requests.post(token_url.format(tenant_id=tenant_id), data=data)

    # Check if the request was successful
    if response.status_code == 200:
        # Extract the access token from the response
        access_token = response.json()['access_token']
        return access_token
    else:
        return -1

In the run_evaluation function before calling the send_question_to_target you have to add

...
logger.info("Getting token...")    
    try:
        token = get_token(tenant_id, client_id, client_secret)
    except Exception as e:
        logger.error("Failed to get token: \n%s", e)
        return False
...
...

Then you have to modify the two calls to the function send_question_to_target adding the retrieved token

target_data = send_question_to_target(
            "What information is in your knowledge base?", "So much", target_url, token, target_parameters, raise_error=True
        )

Finally you have to modify the send_question_to_target as follows:

def send_question_to_target(question: str, truth: str, target_url: str, access_token: str, parameters: dict = {}, raise_error=False):
    headers = {"Content-Type": "application/json",
               "Authorization": "Bearer " + access_token  # Include the access token in the Authorization header
               }
   ...
   ...

Here below you may find a script that test the retrieval of the token.
You have just to modify the tenand_id, client_id, client_secret.

import requests

# Your Azure AD tenant ID
tenant_id = your_tenant_id

# Your Azure AD application (client) ID
client_id = your_client_id

# Your Azure AD application (client) secret
client_secret = your_client_secret

# Azure AD OAuth 2.0 token endpoint
token_url = "https://login.microsoftonline.com/{tenant_id}/oauth2/token"

# Request body parameters
data = {
    'grant_type': 'client_credentials',
    'client_id': client_id,
    'client_secret': client_secret,
    'resource': "api://" + client_id
}

# Make a POST request to the token endpoint
response = requests.post(token_url.format(tenant_id=tenant_id), data=data)

# Check if the request was successful
if response.status_code == 200:
    # Extract the access token from the response
    access_token = response.json()['access_token']
    print("Access Token:", access_token)
else:
    print("Failed to retrieve access token. Status code:", response.status_code)

from azure-search-openai-demo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.