-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBaseLLM.py
134 lines (90 loc) · 3.26 KB
/
BaseLLM.py
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
# ==============================================================================
# Copyright 2023 VerifAI All Rights Reserved.
# https://www.verifai.ai
# License:
#
# ==============================================================================
import os
import sys
import pymongo
from typing import List, Dict
sys.path.append(os.path.join(os.path.dirname(__file__), "."))
from Prompt import *
from Redis import *
"""
The BaseModel class declares several attributes such as model, roles, messages, temp, api_key, max_tokens, and args.
It also defines two methods: __init__() and get_response().
The __init__() method is empty and does not perform any action.
The get_response() method is a placeholder that needs to be implemented by the user.
"""
class BaseLLM(object):
model: str
roles: List[str]
messages: List[List[str]]
temp: float
api_key: str
max_tokens: int
args: str
def __init__(self, **kwargs):
# if values are specified in **kwargs, over-ride defaults
try:
self.name = kwargs['name']
except:
pass
# set credentials json file
try:
self.credentials = kwargs['credentials']
except:
pass
# set default model name
try:
self.model = kwargs['model']
except:
pass
# set class name
try:
self.class_name = kwargs['class_name']
except:
pass
def get_response(self, Prompt):
# Pass in Prompt object and run model with prompt
return
def get_content(self, response):
# Implementer needs to write interface for this
return
def is_code(self, response):
import re
regex_pattern = r"```(?:[a-zA-Z]+)?(?:[a-zA-Z]+\n)?([^`]+)(?:```)?"
matches = re.findall(regex_pattern, response)
if matches:
return True
else:
return False
def publish_to_redis(self, response, taskid):
#publish to redis
if taskid:
meta_data = {"type": "response", "model_name": self.__class__.__name__}
Redis.publish_to_redis(type="multillm",
taskid=taskid,
result=response,
meta_data=meta_data)
def get_conversation_history(self, convid, mod_name):
if os.getenv("MONGO_URI"):
MONGO_URI = os.getenv("MONGO_URI")
else:
MONGO_URI = "mongodb://localhost:27017"
client = pymongo.MongoClient(MONGO_URI)
#Select the database
db = client["verifai"] # Replace "db" with your database name
# Select the collection
collection = db["multillm"]
key= "conversationId"
dataset = collection.find({key: convid})
qa_set = []
for data in dataset:
prompt = data["prompt"]
for result in data["results"]:
if "model_name" in result["meta_data"] and result["meta_data"]["model_name"] == mod_name:
content = result["result"]
qa_set.append((prompt,content))
return qa_set