File size: 1,936 Bytes
7a8853f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from typing import Any, Dict, List, Optional
from langchain_core.prompts import SystemMessagePromptTemplate
import json

DEFAULT_SYSTEM_TEMPLATE = """You have access to the following tools:

{tools}

If using tools , You must respond with a JSON object in a JSON codeblock inside think matching the following schema.


```json
[
{{
"tool": <name of the selected tool>,
"tool_input": <parameters for the selected tool, matching the tool's JSON schema>
}}
]
```

"""  # noqa: E501

DEFAULT_RESPONSE_FUNCTION = {
    "name": "__conversational_response",
    "description": (
        "Respond conversationally if no other tools should be called for a given query."
    ),
    "parameters": {
        "type": "object",
        "properties": {
            "response": {
                "type": "string",
                "description": "Conversational response to the user.",
            },
        },
        "required": ["response"],
    },
}
def _function(**kwargs: Any,):
    functions = kwargs.get("functions", [])
    tools=kwargs.get("tools", [])


    if "type" not in tools and "function" not in tools:
        functions=tools
        functions = [
            fn for fn in functions 
        ]
        if not functions:
            raise ValueError(
                'If "function_call" is specified, you must also pass a matching \
    function in "functions".'
            )
    elif "tools" in kwargs:
        functions = [
            fn["function"] for fn in tools
        ]
        # del kwargs["function_call"]
    # elif ""
    # elif not functions:
    # functions.append(DEFAULT_RESPONSE_FUNCTION)
    system_message_prompt_template = SystemMessagePromptTemplate.from_template(
        DEFAULT_SYSTEM_TEMPLATE
    )
    system_message = system_message_prompt_template.format(
        tools=json.dumps(functions, indent=2)
    )
    if "functions" in kwargs:
        del kwargs["functions"]
    return system_message.content