Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
**/__pycache__
**/__pycache__

.VSCodeCounter
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Google Calendar",
"type": "debugpy",
"request": "launch",
"program": "npi/app/google/calendar/calendar.py",
"console": "integratedTerminal"
}
]
}
1 change: 1 addition & 0 deletions credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"installed":{"client_id":"680950921103-6lvpj65p7r86knohc58ctj8p3qlvkksg.apps.googleusercontent.com","project_id":"translation-api-408108","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["http://localhost"]}}
99 changes: 99 additions & 0 deletions examples/calendar-negotiator/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

""" the example of the calendar negotiator"""

import json

from openai import OpenAI

from npi.constants.openai import Role
from npi.app.google.calendar import GoogleCalendar
from npi.app.feedback.console import HumanFeedback

PROMPT = """
Your are a calendar negotiator. You have the ability to schedule meetings with anyone, anywhere, anytime.

You can use the tools to help you to schedule the meeting.

the tools you can use are: Google Calendar, Google Gmail.

## Goolge Calendar

1. Use Google Calendar to check the availability of the user.
2. Use Google Calendar to create an event for the user.
3. Use Google Calendar to retrive the events from the user's calendar.


## Google Gmail

1. Use Google Gmail to send an email to the user to ask if a specific time is ok for attenar.

You need to adhere to the following rules step by step to schedule the meeting:

1. You can only schedule the meeting with the user's available time.
2. You must need to follow user's task
3. You must need to collect the enough information to schedule the meeting, such as user's email address, user's available time, etc.
4. If you think you need to ask the user for more information to fill the properties, you can use the tools to ask the user for more information.
5. If the tools don't have the ability to help you gather the information, you can ask the user for the information directly.
"""


if __name__ == "__main__":
oai = OpenAI(api_key="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")
gc = GoogleCalendar()
hb = HumanFeedback()

msgs = [
{
"role": Role.ROLE_SYSTEM.value,
"content": PROMPT,
},
{
"role": Role.ROLE_USER.value,
"content": "schedule a meeting with daofeng for Q2 OKR discussion",
}
]
# tools = []
# for func in functions.copy().values():
# tools.append({"type": "function", "function": func})

tools = []
tools.append(gc.as_tool())
tools.append(hb.as_tool())
# print(json.dumps(tools))
response = oai.chat.completions.create(
model="gpt-4-turbo-preview",
messages=msgs,
tools=tools,
tool_choice="auto",
)

functions = {
"google-calendar": gc.chat,
"human": hb.chat,
}
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
while tool_calls is not None:
msgs.append(response_message)
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = functions[function_name]
function_response = function_to_call(
json.loads(tool_call.function.arguments))
msgs.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
)
second_response = oai.chat.completions.create(
model="gpt-4-turbo-preview",
messages=msgs,
tools=[gc.as_tool(), hb.as_tool()],
tool_choice="auto",
) # get a new response from the model where it can see the function response
response_message = second_response.choices[0].message
tool_calls = response_message.tool_calls
print(response_message)
64 changes: 64 additions & 0 deletions npi/app/feedback/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

from npi.core.api import App


class HumanFeedback(App):
"""the function wrapper of HumanFeedback App"""

__human_funcs = {
"ask": {
"name": "ask",
"description": "ask the user for the information",
"parameters": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "the question you want to ask the user"
},
},
"required": ["question"],
},
},
"confirm": {
"name": "confirm",
"description": "confirm the information with the user",
"parameters": {
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "the message you want to confirm with the user"
},
},
"required": ["message"],
},
},
"provide": {
"name": "provide",
"description": "provide the information to the user",
"parameters": {
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "the message you want to provide to the user"
},
},
"required": ["message"],
},
},
}

def __init__(self):
super().__init__(
name="human",
description="if you think other tools can't help you, you can ask the human for help by use this tool.",
llm=None,
mode="gpt-4-turbo-preview",
tool_choice="auto"
)
self._register_functions(self.__human_funcs)

def chat(self, message, context=None) -> str:
return input(message)
Loading