File size: 4,067 Bytes
5ff5c8e
035598f
e3a2236
 
035598f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import time
import json
import requests 
from bs4 import BeautifulSoup

def llm_normal(question,llm):
	print(question)
	start = time.time()
	answer = llm.create_chat_completion(
	        messages= [
    			{
        		"role": "user",
        		"content": question
    			}
			]		
	    )
	end = time.time()
	total_time = end - start
	print(answer["choices"][0]["message"]['content'])
	print(f"Execution time: {total_time:.2f} seconds")
	return answer["choices"][0]["message"]['content']


def llm_agent(question,llm):
	print(question)
	start = time.time()
	answer = llm.create_chat_completion(
	        messages= [
    			{
        		"role": "system",
        		"content": """
                You are a json formatter assistant, 
                specialized in converting user-supplied raw text into json format
                """
    		},{
        		"role": "user",
        		"content": question+"""
				format in JSON: """
    			}
			],response_format = {"type":"json_object"}		
	    )
	end = time.time()
	total_time = end - start
	try:
		args = json.loads(answer["choices"][0]["message"]['content'])
		print(args)
		print(f"Execution time: {total_time:.2f} seconds")
	except:
		print(answer["choices"][0]["message"]['content'])
		print(f"Execution time: {total_time:.2f} seconds")
	return answer["choices"][0]["message"]['content']


def name_age(name,age):
    return str(name) + ' - '+str(age)

def llm_functioncalling(question,llm):
	print(question)
	start = time.time()
	answer = llm.create_chat_completion(
    messages = [
            {
            "role": "system",
            "content": """
                    You are a json formatter assistant, 
                    specialized in converting user-supplied raw text into json format
                    """
            },{
            "role": "user",
            "content": question +"""
            format in JSON: """
            }
			],
	tools=[{
		    "type": "function",
		    "function": {
		      "name": "name_age",
		    "description": """return the name and age, if not informed, 
		                      set name None and age 0""",
		      "parameters": {
		        "type": "object",
		        "title": "return the name and age",
		        "properties": {
		          "name": {
		            "title": "Name",
		            "type": "string"
		          },
		          "age": {
		            "title": "Age",
		            "type": "integer"
		          }
		        },
		        "required": [ "name", "age" ]
		      }
		    }
		  }
	],response_format = {"type":"json_object"},
    )
	end = time.time()
	total_time = end - start
	try:
		args = json.loads(answer["choices"][0]["message"]['content'])
		print(name_age(args['name'],args['age']))
		print(f"Execution time: {total_time:.2f} seconds")
		return name_age(args['name'],args['age'])
	except:
		print(answer["choices"][0]["message"]['content'])
		print(f"Execution time: {total_time:.2f} seconds.")
		return answer["choices"][0]["message"]['content']



def search(question):
	payload = {'q': question}
	request = requests.get('https://www.bing.com/search?&cc=US', params=payload,
		headers = {
		'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
		})
	soup = BeautifulSoup(request.text, 'html.parser')
	try:	 
		text = 'context : '+ str(soup.text.split('·')[1].split('https://')[0])
		return text
	except:
		return ' '




def llm_search(question,llm):
	print(question)
	start = time.time()
	messages = [
	    {
	        "role": "system",
	        "content": """You are a search assistant, gives helpful, 
            detailed, and polite answers to the user's questions
	        """
	    },
	    {
	        "role": "user",
	        "content": question+"""
	        """+search(question)
	    },
	]
	answer = llm.create_chat_completion(
		messages = messages
	)
	end = time.time()
	total_time = end - start
	print(answer["choices"][0]["message"]['content'])
	print(f"Execution time: {total_time:.2f} seconds")
	return answer["choices"][0]["message"]['content']