thanhnt-cf commited on
Commit
17fbdc8
·
verified ·
1 Parent(s): fba754c

update known data

Browse files
Files changed (1) hide show
  1. llm_api/anthropic_api.py +70 -63
llm_api/anthropic_api.py CHANGED
@@ -1,20 +1,32 @@
1
- from anthropic import Anthropic
2
  from dotenv import load_dotenv
 
3
  from llm_api.utils import get_data_format, get_image_data
4
- from .constants import EXTRACT_INFO_HUMAN_MESSAGE, EXTRACT_INFO_SYSTEM_MESSAGE,FOLLOW_SCHEMA_HUMAN_MESSAGE, FOLLOW_SCHEMA_SYSTEM_MESSAGE
5
- from anthropic import APIStatusError
 
 
 
 
 
6
 
7
  load_dotenv(override=True)
8
  client = Anthropic()
 
 
 
 
 
 
9
 
10
- def extract_info(img_paths, schema):
11
- print('Extracting info via Anthropic...')
12
  tools = [
13
  {
14
  "name": "extract_garment_info",
15
  "description": "Extracts key information from the image.",
16
  "input_schema": schema.model_json_schema(),
17
- "cache_control": {"type": "ephemeral"}
18
  }
19
  ]
20
 
@@ -24,105 +36,100 @@ def extract_info(img_paths, schema):
24
  "source": {
25
  "type": "base64",
26
  "media_type": f"image/{get_data_format(img_path)}",
27
- "data": get_image_data(img_path)
28
- }
29
- } for img_path in img_paths
30
- ]
31
-
32
- system_message = [
33
- {
34
- "type": "text",
35
- "text": EXTRACT_INFO_SYSTEM_MESSAGE
36
  }
 
37
  ]
38
 
39
- text_messages = [{
40
- "type": "text",
41
- "text": EXTRACT_INFO_HUMAN_MESSAGE,
42
- }]
43
 
44
- messages=[
45
  {
46
- "role": "user",
47
- "content": text_messages + image_messages
48
  }
49
  ]
50
 
 
 
 
 
 
 
 
 
 
 
51
  try:
52
  response = client.messages.create(
53
- model="claude-3-5-sonnet-20240620",
54
- extra_headers={
55
- "anthropic-beta": "prompt-caching-2024-07-31"
56
- },
57
  max_tokens=2048,
58
  system=system_message,
59
  tools=tools,
60
- messages=messages
61
  )
62
  except APIStatusError as e:
 
63
  return e.status_code, None
64
 
65
  for content in response.content:
66
- if content.type == 'tool_use':
67
- # print(content.input)
68
- # print(type(content.input))
69
- print('Found tool_use!')
70
  return 200, schema.model_validate(content.input)
71
-
72
- print('ERROR: No tool_use found!')
73
 
 
74
 
75
- def follow_structure(json_info, schema):
76
- print('Following structure via Anthropic...')
 
77
  tools = [
78
  {
79
  "name": "extract_garment_info",
80
  "description": FOLLOW_SCHEMA_HUMAN_MESSAGE,
81
  "input_schema": schema.model_json_schema(),
82
- "cache_control": {"type": "ephemeral"}
83
  }
84
  ]
85
 
86
- print('DEBUG: human message**************************')
87
  print(FOLLOW_SCHEMA_HUMAN_MESSAGE.format(json_info=json_info))
88
- text_messages = [{
89
- "type": "text",
90
- "text": FOLLOW_SCHEMA_HUMAN_MESSAGE.format(json_info=json_info),
91
- }]
92
-
93
- system_message = [
94
  {
95
- "type": "text",
96
- "text": FOLLOW_SCHEMA_SYSTEM_MESSAGE
97
  }
98
  ]
99
 
100
- messages=[
101
- {
102
- "role": "user",
103
- "content": text_messages
104
- }
105
- ]
 
 
 
 
 
106
  try:
107
  response = client.messages.create(
108
- model="claude-3-5-sonnet-20240620",
109
- extra_headers={
110
- "anthropic-beta": "prompt-caching-2024-07-31"
111
- },
112
  max_tokens=2048,
113
  system=system_message,
114
  tools=tools,
115
- messages=messages
116
  )
117
  except APIStatusError as e:
 
118
  return e.status_code, None
119
 
120
  for content in response.content:
121
- if content.type == 'tool_use':
122
- # print(content.input)
123
- # print(type(content.input))
124
- print('Found tool_use!***********************')
125
  print(content.input)
126
- return 200, schema.model_validate(content.input['json_info'])
127
-
128
- print('ERROR: No tool_use found!')
 
1
+ from anthropic import Anthropic, APIStatusError
2
  from dotenv import load_dotenv
3
+
4
  from llm_api.utils import get_data_format, get_image_data
5
+
6
+ from .constants import (
7
+ EXTRACT_INFO_HUMAN_MESSAGE,
8
+ EXTRACT_INFO_SYSTEM_MESSAGE,
9
+ FOLLOW_SCHEMA_HUMAN_MESSAGE,
10
+ FOLLOW_SCHEMA_SYSTEM_MESSAGE,
11
+ )
12
 
13
  load_dotenv(override=True)
14
  client = Anthropic()
15
+ # claude_model = 'claude-3-5-sonnet-20240620'
16
+ claude_model = "claude-3-5-sonnet-latest"
17
+ # claude_model = 'claude-3-5-haiku-latest'
18
+ # claude_model = 'claude-3-5-haiku-20241022'
19
+ # claude_model = 'claude-3-haiku-20240307'
20
+
21
 
22
+ def extract_info(img_paths, schema, known_data=None):
23
+ print("Extracting info via Anthropic...")
24
  tools = [
25
  {
26
  "name": "extract_garment_info",
27
  "description": "Extracts key information from the image.",
28
  "input_schema": schema.model_json_schema(),
29
+ "cache_control": {"type": "ephemeral"},
30
  }
31
  ]
32
 
 
36
  "source": {
37
  "type": "base64",
38
  "media_type": f"image/{get_data_format(img_path)}",
39
+ "data": get_image_data(img_path),
40
+ },
 
 
 
 
 
 
 
41
  }
42
+ for img_path in img_paths
43
  ]
44
 
45
+ system_message = [{"type": "text", "text": EXTRACT_INFO_SYSTEM_MESSAGE}]
 
 
 
46
 
47
+ text_messages = [
48
  {
49
+ "type": "text",
50
+ "text": EXTRACT_INFO_HUMAN_MESSAGE,
51
  }
52
  ]
53
 
54
+ if known_data is not None:
55
+ text_messages.append(
56
+ {
57
+ "type": "text",
58
+ "text": f'\nAlso exploit the known data: \n\n"{known_data}"',
59
+ }
60
+ )
61
+
62
+ messages = [{"role": "user", "content": text_messages + image_messages}]
63
+
64
  try:
65
  response = client.messages.create(
66
+ model=claude_model,
67
+ extra_headers={"anthropic-beta": "prompt-caching-2024-07-31"},
 
 
68
  max_tokens=2048,
69
  system=system_message,
70
  tools=tools,
71
+ messages=messages,
72
  )
73
  except APIStatusError as e:
74
+ print(f"{e=}")
75
  return e.status_code, None
76
 
77
  for content in response.content:
78
+ if content.type == "tool_use":
79
+ print("Found tool_use!")
 
 
80
  return 200, schema.model_validate(content.input)
 
 
81
 
82
+ print("ERROR: No tool_use found!")
83
 
84
+
85
+ def follow_structure(json_info, schema, known_data=None):
86
+ print("Following structure via Anthropic...")
87
  tools = [
88
  {
89
  "name": "extract_garment_info",
90
  "description": FOLLOW_SCHEMA_HUMAN_MESSAGE,
91
  "input_schema": schema.model_json_schema(),
92
+ "cache_control": {"type": "ephemeral"},
93
  }
94
  ]
95
 
96
+ print("DEBUG: human message**************************")
97
  print(FOLLOW_SCHEMA_HUMAN_MESSAGE.format(json_info=json_info))
98
+ text_messages = [
 
 
 
 
 
99
  {
100
+ "type": "text",
101
+ "text": FOLLOW_SCHEMA_HUMAN_MESSAGE.format(json_info=json_info),
102
  }
103
  ]
104
 
105
+ if known_data is not None:
106
+ text_messages.append(
107
+ {
108
+ "type": "text",
109
+ "text": f'\nAlso exploit the known data: \n\n"{known_data}"',
110
+ }
111
+ )
112
+
113
+ system_message = [{"type": "text", "text": FOLLOW_SCHEMA_SYSTEM_MESSAGE}]
114
+
115
+ messages = [{"role": "user", "content": text_messages}]
116
  try:
117
  response = client.messages.create(
118
+ model=claude_model,
119
+ extra_headers={"anthropic-beta": "prompt-caching-2024-07-31"},
 
 
120
  max_tokens=2048,
121
  system=system_message,
122
  tools=tools,
123
+ messages=messages,
124
  )
125
  except APIStatusError as e:
126
+ print(f"{e=}")
127
  return e.status_code, None
128
 
129
  for content in response.content:
130
+ if content.type == "tool_use":
131
+ print("Found tool_use!***********************")
 
 
132
  print(content.input)
133
+ return 200, schema.model_validate(content.input["json_info"])
134
+
135
+ print("ERROR: No tool_use found!")