Skip to content

Instantly share code, notes, and snippets.

@dhinag
Last active April 13, 2023 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhinag/8761b4f46d578c2dff3116e0be60a467 to your computer and use it in GitHub Desktop.
Save dhinag/8761b4f46d578c2dff3116e0be60a467 to your computer and use it in GitHub Desktop.
import argparse
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
import json
from langchain.agents import Tool
from flask import Flask
from flask_cors import CORS
from flask import *
import json, time
llm = OpenAI(temperature=0)
app = Flask(__name__)
CORS(app)
@app.route('/request/admin/', methods=['GET'])
def home_page():
query = str(request.args.get('question')) #/request/admin/?question=Dhina
agent_response = agent.run(query)
data_set = {'Message': agent_response}
json_dump = json.dumps(data_set)
return json_dump
if __name__ == "__main__":
app.run(port=7778)
<div class="container">
<h3 class="display-7">Welcome to Power Platform Admin Copilot</h3>
<div class="form-group">
<p>
<textarea id="txtAction" name="input" class="form-control col-md-6" placeholder="What action do you want me to take?"></textarea>
</p>
<p><button id="btnAction" class="btn btn-primary">Submit</button> </p>
<label id="lblResult" for="static-info" class="control-label text-left p-3 mb-2 bg-success text-white" style="display: none;"></label>
</div>
</div>
<script src="<https://code.jquery.com/jquery-3.6.4.min.js>" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script>
<script type="text/javascript">
$("#btnAction").click(function()
{
$("#lblResult").show();
//call agent from Python agent
$("#lblResult").text("Using LLM to determine the actions required and in what order...")
$.get( "<https://localhost:7778/request/admin/?question=>" + $("#txtAction").val())
.done(function( data ) {
var obj = jQuery.parseJSON(data);
$("#lblResult").text(obj.Message);
});
});
</script>
tools = [
    Tool(
    name="Assign Security Role", func=assign_security_role, description=description_sr
    )]
request_format_sr = '{{"user":"<user name>","role":"<role>"}}'
description_sr = f'helps to assign security role to the user. For example, if you get a query "assign security role system administrator to the user Dhina", the role is System Administrator and Dhina is the user name. Another example would be "assign the role Supervisor to John", then Supervisor is the role and John is the user name. Input should be JSON in the following format: {request_format_sr}'
def assign_security_role_DVAPI(user: str = None, role: str = None) -> str:
print("\nCalling Dataverse API to assign '" + role + "' security role to user '" + user + "'")
def assign_security_role(json_request: str) -> str:
arguments_dictionary = json.loads(json_request)
user = arguments_dictionary["user"]
role = arguments_dictionary["role"]
return assign_security_role_DVAPI(user=user, role=role)
def assign_security_role_DVAPI(user: str = None, role: str = None) -> str:
print("\nCalling Dataverse API to assign '" + role + "' security role to user '" + user + "'")
agent = initialize_agent(
tools,
llm,
agent="zero-shot-react-description",
verbose=True
)
parser = argparse.ArgumentParser(description='agent calling power platform api to perform actions')
parser.add_argument('-q', '--question', dest='question', type=str, help='question to submit to the agent. Enclose the question sentence in quotes.', required=True)
args = parser.parse_args()
agent.run(args.question)
python lc-agent-cli/lc_pp_cli_agent.py -q 'assign system administrator role to the user dhina'
python lc-agent-cli/lc_pp_cli_agent.py -q 'assign system administrator role to the user dhina and add to the team Contact Center Agents'
#parser = argparse.ArgumentParser(description='agent calling power platform api to perform actions')
#parser.add_argument('-q', '--question', dest='question', type=str, help='question to submit to the agent. Enclose the question sentence in quotes.', required=True)
#args = parser.parse_args()
#agent.run(args.question)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment