forked from AntonOsika/gpt-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ai.py
66 lines (47 loc) · 1.9 KB
/
test_ai.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
from gpt_engineer.core.ai import AI
from langchain.chat_models.fake import FakeListChatModel
from langchain.chat_models.base import BaseChatModel
def mock_create_chat_model(self) -> BaseChatModel:
return FakeListChatModel(responses=["response1", "response2", "response3"])
def mock_check_model_access_and_fallback(self, model_name):
return model_name
def test_start(monkeypatch):
# arrange
monkeypatch.setattr(
AI, "_check_model_access_and_fallback", mock_check_model_access_and_fallback
)
monkeypatch.setattr(AI, "_create_chat_model", mock_create_chat_model)
ai = AI("gpt-4")
# act
response_messages = ai.start("system prompt", "user prompt", "step name")
# assert
assert response_messages[-1].content == "response1"
def test_next(monkeypatch):
# arrange
monkeypatch.setattr(
AI, "_check_model_access_and_fallback", mock_check_model_access_and_fallback
)
monkeypatch.setattr(AI, "_create_chat_model", mock_create_chat_model)
ai = AI("gpt-4")
response_messages = ai.start("system prompt", "user prompt", "step name")
# act
response_messages = ai.next(
response_messages, "next user prompt", step_name="step name"
)
# assert
assert response_messages[-1].content == "response2"
def test_token_logging(monkeypatch):
# arrange
monkeypatch.setattr(
AI, "_check_model_access_and_fallback", mock_check_model_access_and_fallback
)
monkeypatch.setattr(AI, "_create_chat_model", mock_create_chat_model)
ai = AI("gpt-4")
# act
response_messages = ai.start("system prompt", "user prompt", "step name")
usageCostAfterStart = ai.token_usage_log.usage_cost()
ai.next(response_messages, "next user prompt", step_name="step name")
usageCostAfterNext = ai.token_usage_log.usage_cost()
# assert
assert usageCostAfterStart > 0
assert usageCostAfterNext > usageCostAfterStart