forked from i-am-alice/2nd-devs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
13.py
111 lines (93 loc) · 4.06 KB
/
13.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from langchain_openai import ChatOpenAI
from langchain.schema import BaseMessage, HumanMessage
import json
query_enrichment_schema = {
"name": "query_enrichment",
"description": "Describe users query with semantic tags and classify with type",
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "boolean",
"description": "Set to 'true' when query is direct command for AI. Set to 'false' when queries asks for saying/writing/translating/explaining something and all other."
},
"type": {
"type": "string",
"description": "memory (queries about the user and/or AI), notes|links (queries about user's notes|links). By default pick 'memory'.",
"enum": ["memory", "notes", "links"]
},
"tags": {
"type": "array",
"description": "Multiple semantic tags/keywords that enriches query for search purposes (similar words, meanings). When query refers to the user, add 'overment' tag, and when refers to 'you' add tag 'Alice'",
"items": {
"type": "string"
}
}
},
"required": [
"type", "tags", "command"
]
}
}
# --------------------------------------------------------------
# OWN TEST - Example without using funciton_call
# --------------------------------------------------------------
# kwarg function_call is used to selectdefault function that should be used by model
# if skipped we need to suggest model to use it in the user prompt
# if provided then model will call funciton by default
model = ChatOpenAI(
model_name="gpt-4-0613",
model_kwargs={
"functions": [query_enrichment_schema]
# "function_call": {"name": "query_enrichment"},
}
)
result = model([HumanMessage(content="Hey there!")])
print(json.dumps(json.loads(result.json()), indent=4)) #Returns just normal model greeting
result = model([HumanMessage(content="Describe this query: I just want to say HELLO!")])
print(json.dumps(json.loads(result.json()), indent=4)) #returns desired schema
# --------------------------------------------------------------
# /OWN TEST - Example without using funciton_call
# --------------------------------------------------------------
model = ChatOpenAI(
model_name="gpt-4-0613",
model_kwargs={
"functions": [query_enrichment_schema],
"function_call": {"name": "query_enrichment"},
}
)
print(json.dumps({
"functions": [query_enrichment_schema],
"function_call": {"name": "query_enrichment"},
}, indent=4))
result = model([HumanMessage(content="Hey there!")])
# --------------------------------------------------------------
# extract desired data
# --------------------------------------------------------------
def parse_function_call(result: BaseMessage):
if result.additional_kwargs and "function_call" in result.additional_kwargs:
return {
"name": result.additional_kwargs["function_call"]["name"],
"args": json.loads(result.additional_kwargs["function_call"]["arguments"]),
}
return None
action = parse_function_call(result)
if action:
print(action["name"], action["args"])
#############################################################################
# ------------- Add part to call the function basing on model response
#############################################################################
possible_functions = {}
def query_enrichment(command, type, tags, **kwargs):
"""
It should have some connection to database and use formated output
in some meaningful way, however I am just presenting how we can call function
by now. Final implementetion is not important here
"""
print("Command: ", command, "\ntype: ", type, "\nTags: ",tags)
possible_functions['query_enrichment'] = query_enrichment
# just for readability
func_name = action["name"]
func_args = action["args"]
if action and func_name in possible_functions:
possible_functions[func_name](**func_args)