|
import os
|
|
import os
|
|
from pathlib import Path
|
|
import csv
|
|
import json
|
|
import openai
|
|
import time
|
|
import pandas as pd
|
|
|
|
|
|
api_key = "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"
|
|
|
|
openai.api_key = api_key
|
|
|
|
|
|
model_engine = "text-davinci-003"
|
|
import gradio as gr
|
|
import time
|
|
import argparse
|
|
from vllm import LLM, SamplingParams
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--model", type=str)
|
|
parser.add_argument("--n_gpu", type=int, default=1)
|
|
return parser.parse_args()
|
|
|
|
def echo(message, history, system_prompt, temperature, max_tokens):
|
|
response = f"System prompt: {system_prompt}\n Message: {message}. \n Temperature: {temperature}. \n Max Tokens: {max_tokens}."
|
|
for i in range(min(len(response), int(max_tokens))):
|
|
time.sleep(0.05)
|
|
yield response[: i+1]
|
|
|
|
|
|
|
|
def align_data(data):
|
|
"""Given dict with lists, creates aligned strings
|
|
|
|
Adapted from Assignment 3 of CS224N
|
|
|
|
Args:
|
|
data: (dict) data["x"] = ["I", "love", "you"]
|
|
(dict) data["y"] = ["O", "O", "O"]
|
|
|
|
Returns:
|
|
data_aligned: (dict) data_align["x"] = "I love you"
|
|
data_align["y"] = "O O O "
|
|
|
|
"""
|
|
spacings = [max([len(seq[i]) for seq in data.values()])
|
|
for i in range(len(data[list(data.keys())[0]]))]
|
|
data_aligned = dict()
|
|
|
|
|
|
for key, seq in data.items():
|
|
str_aligned = ""
|
|
for token, spacing in zip(seq, spacings):
|
|
str_aligned += token + " " * (spacing - len(token) + 1)
|
|
|
|
data_aligned[key] = str_aligned
|
|
|
|
return data_aligned
|
|
|
|
|
|
def get_llm_result(input_sys_prompt_str, input_history_str, prompt_str):
|
|
|
|
prompt = ""
|
|
|
|
def predict(message, history, system_prompt, temperature, max_tokens):
|
|
instruction = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. "
|
|
for human, assistant in history:
|
|
instruction += 'USER: '+ human + ' ASSISTANT: '+ assistant + '</s>'
|
|
instruction += 'USER: '+ message + ' ASSISTANT:'
|
|
problem = [instruction]
|
|
stop_tokens = ["Question:", "Question", "USER:", "USER", "ASSISTANT:", "ASSISTANT", "Instruction:", "Instruction", "Response:", "Response"]
|
|
sampling_params = SamplingParams(temperature=temperature, top_p=1, max_tokens=max_tokens, stop=stop_tokens)
|
|
completions = llm.generate(problem, sampling_params)
|
|
for output in completions:
|
|
prompt = output.prompt
|
|
generated_text = output.outputs[0].text
|
|
return generated_text
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_path = "/workspaceblobstore/caxu/trained_models/13Bv2_497kcontinueroleplay_dsys_2048_e4_2e_5/checkpoint-75"
|
|
llm = LLM(model=model_path, tensor_parallel_size=1)
|
|
history = input_history_str
|
|
prompt = prompt_str
|
|
system_prompt = input_sys_prompt_str
|
|
|
|
response = predict(prompt, history, system_prompt, 0.5, 3000)
|
|
|
|
print(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response, response
|
|
|
|
except Exception as ex:
|
|
print("File not exist")
|
|
raise ex
|
|
|
|
def get_model_api():
|
|
"""Returns lambda function for api"""
|
|
|
|
|
|
def model_api(input_sys_prompt_str, input_history_str, prompt_str):
|
|
"""
|
|
Args:
|
|
input_data: submitted to the API, raw string
|
|
|
|
Returns:
|
|
output_data: after some transformation, to be
|
|
returned to the API
|
|
|
|
"""
|
|
|
|
|
|
|
|
punc = [",", "?", ".", ":", ";", "!", "(", ")", "[", "]"]
|
|
|
|
|
|
preds, title_preds = get_llm_result(input_sys_prompt_str, input_history_str, prompt_str)
|
|
output_data = {"system_prompt": input_sys_prompt_str, "history": input_history_str, "USER": prompt_str, "ASSISTANT": preds}
|
|
return output_data
|
|
|
|
return model_api
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|