Spaces:
No application file
No application file
| """ | |
| Data Scientist.: Dr.Eddy Giusepe Chirinos Isidro | |
| Neste script vamos aprender a obter Dados de saída de forma estruturada usando | |
| Function Calling. Basicamente fazemos os resumo de um texto. | |
| """ | |
| import json | |
| from termcolor import colored | |
| import logging | |
| logging.basicConfig(level=logging.INFO) | |
| #Substitua sua chave de API OpenAI: | |
| import openai | |
| import os | |
| from dotenv import load_dotenv, find_dotenv | |
| _ = load_dotenv(find_dotenv()) # read local .env file | |
| openai.api_key = os.environ['OPENAI_API_KEY'] | |
| model = "gpt-3.5-turbo-16k-0613" # model = "gpt-4-0613" | |
| def summarize_text(text, model): | |
| response = openai.ChatCompletion.create(model = model, | |
| messages = [{"role": "user", "content": f"Resumir: {text}"}, | |
| ], | |
| # stream = True, | |
| functions = [ | |
| { | |
| "name": "resumir_documento", | |
| "description": "Resume um documento retornando um resumo", | |
| "parameters": {"type": "object", | |
| "properties": {"summary": {"type": "string", | |
| "description": "um breve resumo do documento." | |
| } | |
| }, | |
| "required": ["summary"] | |
| } | |
| } | |
| ], | |
| function_call = {"name": "resumir_documento"} # Pode ser "auto" or "none" | |
| ) | |
| logging.info("*** 🤗 Imprimindo a resposta raw 🤗 ***") | |
| print(colored(response, "green")) | |
| response_message = response["choices"][0]["message"] | |
| if response_message.get("function_call"): | |
| function_name = response_message["function_call"]["name"] | |
| function_args = json.loads(response_message["function_call"]["arguments"]) | |
| print(colored(response_message, "yellow")) | |
| print(colored(function_args["summary"], "blue")) | |
| print("") | |
| logging.info("*** 🤗 Imprimindo o RESUMO 🤗 ***") | |
| return function_args['summary'] | |
| if __name__ == "__main__": | |
| # Leia o conteúdo do .txt: | |
| with open('/home/eddygiusepe/1_Eddy_Giusepe/6_REPO_HuggingFace/17_StreamingResponses_FunctionCalling_Summary_KeyWord/sample.txt', 'r') as file: | |
| sample = file.read() | |
| print("") | |
| print(colored(summarize_text(sample, model), "red")) | |
| print("") | |
| logging.info("🦜 O texto ORIGINAL antes de ser resumido 🦜") | |
| print(colored(sample, "green")) |