Coverage for tests/test_openai.py: 75%

59 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-22 15:39 -0800

1"""Testing openai API calls 

2 

3NOTE: The tests could cost money if you have an openai key set. 

4NOTE: Some tests might fail due to randomness in the API. 

5You can skip these tests by setting the environment variable SKIP_MODEL_CALL_TESTS=1 

6""" 

7 

8import os 

9import sys 

10 

11import pytest 

12from loguru import logger 

13from pydantic import BaseModel 

14 

15import appl 

16from appl import AIRole, Generation, Image, UserRole, as_tool, gen, ppl 

17from appl.const import NEWLINE 

18 

19appl.init() 

20# NOTE: init here could influence other tests in other files. 

21 

22 

23@ppl 

24def add(): 

25 "1+2=" 

26 return str(gen("gpt4o-mini", max_tokens=10)).strip() 

27 

28 

29try: 

30 add() 

31 reason = None 

32 openai_api_callable = True 

33except Exception as e: 

34 reason = str(e) 

35 openai_api_callable = False 

36 

37skip_tests = os.environ.get("SKIP_LLM_CALL_TESTS") == "1" 

38pytestmark = [ 

39 pytest.mark.skipif(skip_tests, reason="Skipped due to setting"), 

40 pytest.mark.skipif( 

41 not openai_api_callable, 

42 reason=f"Error encountered when calling openai API: {reason}", 

43 ), 

44] 

45 

46 

47# @pytest.mark.skipif(skip_tests, reason="Skipped due to setting") 

48def test_openai(): 

49 assert "3" in add() 

50 

51 

52def test_tool_call(): 

53 def special_number(x: int) -> int: 

54 """A function to calculate the special number of an integer.""" 

55 return x**2 + 1 

56 

57 tools = [as_tool(special_number)] 

58 

59 @ppl 

60 def func(x: int): 

61 f"What's the special number of {x}?" 

62 res = gen(tools=tools) 

63 return [x.get_content() for x in res.run_tool_calls()] 

64 

65 assert func(5) == [special_number(5)] 

66 

67 

68IMAGE_URL = "https://maproom.wpenginepowered.com/wp-content/uploads/OpenAI_Logo.svg_-500x123.png" 

69 

70 

71def test_image(): 

72 @ppl 

73 def query(): 

74 "Look, it's a commercial logo." 

75 Image(IMAGE_URL) 

76 "What's the text on the image? " 

77 "Your output should be in the format: The text on the image is: ..." 

78 return gen("gpt4o-mini", stop=NEWLINE) 

79 

80 assert "OpenAI" in str(query()) 

81 

82 

83def test_response_format(): 

84 class Result(BaseModel): 

85 result: int 

86 

87 @ppl 

88 def func(): 

89 "1+1=" 

90 return gen("gpt4o-mini", response_format=Result).response_obj 

91 

92 assert func().result == 2 

93 

94 @ppl 

95 def func2(): 

96 "1+1=" 

97 return gen("gpt4o-mini", response_format=Result, stream=True).response_obj 

98 

99 assert func2().result == 2 

100 

101 

102def test_auto_continue(): 

103 @ppl 

104 def func(): 

105 "Count from 1 to 50, in a format like 1, 2, ..." 

106 "BEGIN: " 

107 return gen("gpt4o-mini", max_tokens=100, max_relay_rounds=5) 

108 

109 assert "45, 46, 47" in str(func())