-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathutil.py
More file actions
162 lines (137 loc) · 5.59 KB
/
util.py
File metadata and controls
162 lines (137 loc) · 5.59 KB
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
155
156
157
158
159
160
161
162
"""Utility methods to load movie data from data files.
Ported to Python 3 by Matt Mistele (@mmistele) and Sam Redmond (@sredmond).
Intended for PA7 in Stanford's CS124.
"""
import csv
import json
from typing import Tuple, List, Dict
from functools import lru_cache
import numpy as np
from openai import OpenAI, APIConnectionError
DEFAULT_STOP = ["\n\n\n\n\n", "<</SYS>>"]
def load_ratings(src_filename, delimiter: str = '%',
header: bool = False) -> Tuple[List, np.ndarray]:
title_list = load_titles('data/movies.txt')
user_id_set = set()
with open(src_filename, 'r') as f:
content = f.readlines()
for line in content:
user_id = int(line.split(delimiter)[0])
if user_id not in user_id_set:
user_id_set.add(user_id)
num_users = len(user_id_set)
num_movies = len(title_list)
mat = np.zeros((num_movies, num_users))
with open(src_filename) as f:
reader = csv.reader(f, delimiter=delimiter, quoting=csv.QUOTE_MINIMAL)
if header:
next(reader)
for line in reader:
mat[int(line[1])][int(line[0])] = float(line[2])
return title_list, mat
def load_titles(src_filename: str, delimiter: str = '%',
header: bool = False) -> List:
title_list = []
with open(src_filename, 'r', encoding='utf-8') as f:
reader = csv.reader(f, delimiter=delimiter, quoting=csv.QUOTE_MINIMAL)
if header:
next(reader)
for line in reader:
movieID, title, genres = int(line[0]), line[1], line[2]
if title[0] == '"' and title[-1] == '"':
title = title[1:-1]
title_list.append([title, genres])
return title_list
def load_sentiment_dictionary(src_filename: str, delimiter: str = ',',
header: bool = False) -> Dict:
with open(src_filename, 'r') as f:
reader = csv.reader(f, delimiter=delimiter, quoting=csv.QUOTE_MINIMAL)
if header:
next(reader)
return dict(reader)
@lru_cache
def load_together_client():
together_client = None
try:
from api_keys import TOGETHER_API_KEY
together_client = OpenAI(api_key=TOGETHER_API_KEY,
base_url='https://api.together.xyz',
)
except ImportError:
print("\001\033[93m\002WARNING: Unable to load Together API client (TOGETHER_API_KEY in api_keys.py not found)\001\033[0m\002")
print("\001\033[93m\002LLM Calls will not work. Please add a TOGETHER_API_KEY to api_keys.py before starting parts 2 and 3.\001\033[0m\002")
return together_client
def call_llm(messages, client, model="mistralai/Mixtral-8x7B-Instruct-v0.1", max_tokens=256):
chat_completion = client.chat.completions.create(
messages=messages,
model=model,
max_tokens=max_tokens
)
return chat_completion.choices[0].message.content
# model = "meta-llama/Llama-2-70b-chat-hf"
def stream_llm_to_console(messages, client, model="mistralai/Mixtral-8x7B-Instruct-v0.1", max_tokens=256, stop=None):
try:
stream = client.chat.completions.create(
model=model,
messages=messages,
stream=True,
max_tokens=max_tokens,
stop=stop
)
response = ""
for chunk in stream:
response += chunk.choices[0].delta.content or ""
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()
except APIConnectionError as e:
print("\001\033[91m\002ERROR connecting to Together API! Please check your TOGETHER_API_KEY in api_keys.py and try again.\001\033[0m\002")
response = None
return response
### Student Facing API
# Makes a call to the Together API using the given system prompt and user message.
# system_prompt: The system prompt to send to the API.
# message: The user message to send to the API.
# model: The model to use for the API call.
# max_tokens: The maximum number of tokens to generate in the response.
# Returns the response from the API.
def simple_llm_call(system_prompt, message, model="mistralai/Mixtral-8x7B-Instruct-v0.1", max_tokens=256, stop=None):
client = load_together_client()
chat_completion = client.chat.completions.create(
messages=[{
"role": "system",
"content": system_prompt,
}, {
"role": "user",
"content": message,
}],
model=model,
max_tokens=max_tokens,
stop=stop
)
return chat_completion.choices[0].message.content
### Student Facing API
# Makes a call to the Together API using the given system prompt and user message with JSON output formatting.
# system_prompt: The system prompt to send to the API.
# message: The user message to send to the API.
# json_class: The class to use for the JSON output.
# model: The model to use for the API call.
# max_tokens: The maximum number of tokens to generate in the response.
# Returns the response from the API as a JSON object
def json_llm_call(system_prompt, message, json_class, model="mistralai/Mixtral-8x7B-Instruct-v0.1", max_tokens=256):
client = load_together_client()
chat_completion = client.chat.completions.create(
messages=[{
"role": "system",
"content": system_prompt,
}, {
"role": "user",
"content": message,
}],
model=model,
max_tokens=max_tokens,
response_format = {
"type": "json_object",
"schema": json_class.model_json_schema(),
}
)
return json.loads(chat_completion.choices[0].message.content)