-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllms.py
151 lines (134 loc) · 4.93 KB
/
llms.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import requests
import json
from openai import OpenAI
def chatgpt(prompt, model="gpt-3.5-turbo", function_call_flag=1):
answer_generation_function = [
{
"name": "answer_generation_function",
"description": "Answer the given question based on the given context.",
"parameters": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"description": "Answer of the given question",
}
}
}
}
]
entity_encapsulating_phrase_identification_function = [
{
"name": "entity_encapsulating_phrase_identification_function",
"description": "Extract the entity encapsulating phrases and title from the given question.",
"parameters": {
"type": "object",
"properties": {
"entity_encapsulating_phrase": {
"type": "array",
"items": {
"type": "string"
},
"description": "Entity encapsulating phrase in a given question. Be sure to follow the examples given in the prompt",
},
"title": {
"type": "array",
"items": {
"type": "string"
},
"description": "Publication title in a question. Be sure to follow the examples given in the prompt",
}
}
}
}
]
title_extraction_function = [
{
"name": "title_extraction_function",
"description": "Extract the title from the given phrase.",
"parameters": {
"type": "object",
"properties": {
"title": {
"type": "array",
"items": {
"type": "string"
},
"description": "Publication title in a question. Be sure to follow the examples given in the prompt",
}
}
}
}
]
next_hop_phrase_extraction = [
{
"name": "next_hop_phrase_extraction",
"description": "Extract the entity encapsulating phrase from the given question.",
"parameters": {
"type": "object",
"properties": {
"entity_encapsulating_phrase": {
"type": "string",
"description": "Next hop phrase. Be sure to follow the examples given in the prompt",
}
}
}
}
]
text_generation_function = [
{
"name": "text_generation_function",
"description": "Generate a text.",
"parameters": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "Generated Text.",
}
}
}
}
]
if function_call_flag == 4:
function_call = answer_generation_function
elif function_call_flag == 5:
function_call = entity_encapsulating_phrase_identification_function
elif function_call_flag == 6:
function_call = title_extraction_function
elif function_call_flag == 7:
function_call = next_hop_phrase_extraction
elif function_call_flag == 8:
function_call = text_generation_function
client = OpenAI()
completion = client.chat.completions.create(
model=model,
messages=[
{"role": "user", "content": prompt}
],
functions=function_call,
function_call='auto'
)
try:
json_response = json.loads(completion.choices[0].message.function_call.arguments)
return json_response
except Exception as e:
print(f"An error occurred: {e}")
return ""
def llama(user_prompt, sys_prompt_string="You are an experienced annotator."):
server_url = "http://localhost:11434/api/generate"
model = "llama3:instruct" # "instruct" # "llama3:8B"
messages = [
{"role": "system", "content": sys_prompt_string},
{"role": "user", "content": user_prompt}]
data = dict(model=model, prompt=user_prompt, stream=False)
headers = {"content-type": "application/json"}
response = requests.post(server_url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
response_data = response.json()
# print(f"Model: {response_data["model"]}")
# print(f"Response: {response_data["response"]}")
return response_data["response"]
else:
print(f"Error: {response.status_code}")
return None