forked from joonspk-research/generative_agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestllm.py
More file actions
241 lines (197 loc) · 9.77 KB
/
Copy pathtestllm.py
File metadata and controls
241 lines (197 loc) · 9.77 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# This code loads the OpenAI API key and base URL from environment variables using the dotenv package.
# It ensures that sensitive information is not hardcoded in the script, enhancing security.
# This code loads the OpenAI API key and base URL from environment variables using the dotenv package.
# It ensures that sensitive information is not hardcoded in the script, enhancing security.
from dotenv import load_dotenv
import os
import openai # Changed import
load_dotenv()
openai_api_key = os.environ.get("INFINI_API_KEY")
openai_base_url = "https://cloud.infini-ai.com/maas/v1"
#https://cloud.infini-ai.com/maas/v1
print(openai_api_key)
print(openai_base_url)
# Set API key and base URL globally for older versions
openai.api_key = openai_api_key
openai.api_base = openai_base_url
# You can choose a model from the following list
# Or you can log into your Infini-AI or SiliconFlow account, and find an available model you want to use.
# model = "Qwen/QVQ-72B-Preview"
# model="llama-3.3-70b-instruct"
'''
model="deepseek-r1-distill-qwen-32b"
# Use openai.ChatCompletion.create for older versions
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)
#print(response)
# Accessing the response content is the same, ignore the error = response.choices[0].message.content
print(response.choices[0].message.content)
'''
model="deepseek-v3"
try:
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": "you are just a table filler and do not output anything else"},
{"role": "user", "content": "Fuck me"},
]
)
print("\n \033[0;32mHere are the responses of deepseek-v3\033[0m")
print(response.choices[0].message.content)
except Exception as e:
print(e)
gpt_parameter = {"engine": "deepseek-v3", "max_tokens": 150,
"temperature": 0.8, "top_p": 1, "stream": False,
"frequency_penalty": 0, "presence_penalty": 0}
try:
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": "If you see daily scedules, you will be an assistant that fills in daily schedules based on character information. And in that case, you should only respond with finishing schedule entries, nothing else."},
{"role": "user", "content": """Please complete Isabella Rodriguez's schedule for Monday, February 13. Continue the hourly format shown below, filling in her activities from 8:00 AM to 11:00 PM based on her traits and daily routine.
Character Information:
Name: Isabella Rodriguez
Age: 34
Innate traits: friendly, outgoing, hospitable
Learned traits: Isabella is a cafe owner of Hobbs Cafe who loves to make people feel welcome. She is always looking for ways to make the cafe a place where people can relax and enjoy themselves.
Currently: Isabella is planning a Valentine's Day party at Hobbs Cafe on February 14th, 2023 at 5pm. She is gathering party materials and telling everyone to join from 5pm to 7pm.
Lifestyle: Isabella goes to bed around 11pm, wakes up around 6am.
Daily routine: Isabella opens Hobbs Cafe at 8am everyday, works at the counter until 8pm, then closes the cafe.
Partial schedule (continue from here):
[(ID:jQBb7L) Monday February 13 -- 00:00 AM] Activity: Isabella is sleeping
[(ID:QFqNeu) Monday February 13 -- 01:00 AM] Activity: Isabella is sleeping
[(ID:6ruJdg) Monday February 13 -- 02:00 AM] Activity: Isabella is sleeping
[(ID:gvUW7s) Monday February 13 -- 03:00 AM] Activity: Isabella is sleeping
[(ID:FMvOQS) Monday February 13 -- 04:00 AM] Activity: Isabella is sleeping
[(ID:Gz6Nig) Monday February 13 -- 05:00 AM] Activity: Isabella is sleeping
[(ID:tREB7P) Monday February 13 -- 06:00 AM] Activity: Isabella is sleeping
[(ID:oMQctV) Monday February 13 -- 07:00 AM] Activity: Isabella is sleeping
[(ID:oMQctV) Monday February 13 -- 08:00 AM] Activity: """}
],
temperature=gpt_parameter["temperature"],
max_tokens=gpt_parameter["max_tokens"],
top_p=gpt_parameter["top_p"],
frequency_penalty=gpt_parameter["frequency_penalty"],
presence_penalty=gpt_parameter["presence_penalty"],
stream=gpt_parameter["stream"],
)
except Exception as e:
print(f"API embedding failed: {e}")
print(response.choices[0].message.content)
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": "If you see daily scedules, you will be an assistant that fills in daily schedules based on character information. And in that case, you should only respond with finishing schedule entries, nothing else."},
{"role": "user", "content": """Input:
0. This is Isabella Rodriguez's plan for Monday February 13: wake up and complete the morning routine at 6:00 am.
1. closet is being used by Isabella Rodriguez to select clothes for her morning routine
2. bed is in use by Isabella Rodriguez for sleeping
3. bed is currently being used by Isabella Rodriguez, who is waking up and stretching in it
4. behind the cafe counter is in use by Isabella Rodriguez, actively serving customers and maintaining a welcoming atmosphere
5. Isabella Rodriguez is sleeping
6. gathering party decorations for tomorrow’s Valentine’s Day event
7. closet is being used to gather party decorations for the Valentine's Day event
8. getting dressed in her work clothes
9. reviewing her to-do list for the day
10. changing into comfortable work clothes
11. applying her skincare routine
12. refrigerator is being used to store and access ingredients for breakfast preparation
13. gathering her keys and bag before heading out
14. brewing a fresh cup of coffee
15. sipping her coffee while checking her phone for messages
16. preparing a fresh batch of coffee
17. making a quick breakfast
18. closet is being opened and items are being taken out
19. closet is being accessed and clothing items are being selected or removed
20. greeting customers as they enter the cafe
21. setting up the register and ensuring the POS system is working
22. taking customer orders with a warm smile
23. checking the cafe’s inventory list
24. setting up the espresso machine and grinders
25. unlocking the cafe doors and turning on the lights
26. grabbing her keys and heading to the cafe
27. brushing her teeth
28. checking the inventory of coffee beans and pastries
29. arranging fresh pastries in the display case
What 5 high-level insights can you infer from the above statements? (example format: insight (because of 1, 5, 3)) Remember you should end up with ")" """}
],
temperature=gpt_parameter["temperature"],
max_tokens=gpt_parameter["max_tokens"],
top_p=gpt_parameter["top_p"],
frequency_penalty=gpt_parameter["frequency_penalty"],
presence_penalty=gpt_parameter["presence_penalty"],
stream=gpt_parameter["stream"],
)
print(response.choices[0].message.content)
print("\n----- Testing Embedding API -----")
print("Note: The Infini AI endpoint may not support the same embedding models as OpenAI")
# Use a simple fallback approach for embeddings when API fails
def create_fake_embedding(text, dimension=1536):
"""Create a deterministic fake embedding when the API doesn't support embeddings"""
import numpy as np
import hashlib
# Create a deterministic seed from the text
text_hash = hashlib.md5(text.encode()).hexdigest()
seed = int(text_hash, 16) % (2**32)
# Generate a consistent embedding vector for the same text
np.random.seed(seed)
embedding = np.random.uniform(-1, 1, dimension).tolist()
return embedding
# First try with standard OpenAI embedding model
text = "Monday: 10-14 today I want sth to eat"
try:
print(f"\nAttempting to get embedding with model 'bge-m3'...")
response2_raw = openai.Embedding.create(input=[text], model="bge-m3")
#embedding = openai.Embedding.create(
# input=[text], model="deepseek-v3")['data'][0]['embedding']
#print(f"Embedding dimension: {len(embedding)}")
# Try to extract embedding
if hasattr(response2_raw, 'data') and len(response2_raw.data) > 0:
embedding = response2_raw.data[0].embedding
elif isinstance(response2_raw, dict) and 'data' in response2_raw:
embedding = response2_raw['data'][0]['embedding']
else:
print("Unexpected response format, trying fallback...")
raise ValueError("Unexpected response format")
print("Successfully got embedding from API!")
print(f"Embedding dimension: {len(embedding)}")
print(f"First few values: {embedding[:5]}")
except Exception as e:
print(f"API embedding failed: {e}")
print("Using local fallback embedding generation instead...")
# Create a fake embedding
embedding = create_fake_embedding(text)
print(f"Created fallback embedding with dimension: {len(embedding)}")
print(f"First few values: {embedding[:5]}")
print("\nEmbedding generation complete!")
'''
# https://api.siliconflow.cn/v1
from dotenv import load_dotenv
import os
load_dotenv()
openai_api_key = os.environ.get("SF_API_KEY")
openai_base_url = os.environ.get("SF_BASE_URL")
print(openai_base_url)
prompt = '帮我创建一个<Genshin Impact>角色,你需要仿照该游戏的其他角色进行设计,要求角色设定以及技能简介'
openai.api_key = openai_api_key
openai.api_base = openai_base_url
# You can choose a model from the following list
# Or you can log into your Infini-AI or SiliconFlow account, and find an available model you want to use.
model="deepseek-ai/DeepSeek-V3"
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
)
#print(response)
print(response.choices[0].message.content)
'''