Spaces:
Sleeping
Sleeping
| 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'] | |