Skip to content

Commit d497ec2

Browse files
feat: Updated xpert client to newer version
1 parent b14f447 commit d497ec2

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

CHANGELOG.rst

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Change Log
1313
1414
Unreleased
1515

16+
[2.3.0] - 2025-04-29
17+
---------------------
18+
* feat: updated the xpert client to use the new endpoint
19+
1620
[2.2.1] - 2025-05-05
1721
---------------------
1822
* chore: Upgrade python requirements

taxonomy/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
# 2. MINOR version when you add functionality in a backwards compatible manner, and
1616
# 3. PATCH version when you make backwards compatible bug fixes.
1717
# More details can be found at https://semver.org/
18-
__version__ = '2.2.1'
18+
__version__ = '2.3.0'

taxonomy/openai/client.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,30 @@
99
log = logging.getLogger(__name__)
1010

1111

12-
def chat_completion(prompt):
12+
def chat_completion(prompt, system_message=settings.XPERT_DEFAULT_SYSTEM_MESSAGE):
1313
"""
14-
Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API setting.
14+
Pass message list to chat endpoint, as defined by the CHAT_COMPLETION_API_V2 setting.
1515
Arguments:
1616
prompt (str): chatGPT prompt
17+
system_message (str): system message to be used in the chat
1718
"""
18-
completion_endpoint = settings.CHAT_COMPLETION_API
19-
completion_endpoint_key = settings.CHAT_COMPLETION_API_KEY
20-
headers = {'Content-Type': 'application/json', 'x-api-key': completion_endpoint_key}
19+
completion_endpoint = settings.CHAT_COMPLETION_API_V2
20+
headers = {'Content-Type': 'application/json'}
2121
connect_timeout = getattr(settings, 'CHAT_COMPLETION_API_CONNECT_TIMEOUT', 1)
2222
read_timeout = getattr(settings, 'CHAT_COMPLETION_API_READ_TIMEOUT', 15)
23-
body = {'message_list': [{'role': 'assistant', 'content': prompt},]}
23+
body = {
24+
'messages': [{'role': 'assistant', 'content': prompt},],
25+
'client_id': settings.XPERT_CLIENT_ID,
26+
'system_message': system_message,
27+
}
2428
response = requests.post(
2529
completion_endpoint,
2630
headers=headers,
2731
data=json.dumps(body),
2832
timeout=(connect_timeout, read_timeout)
2933
)
30-
chat = response.json().get('content')
34+
if isinstance(response.json(), dict):
35+
chat = response.json().get('content')
36+
else:
37+
chat = response.json()[0].get('content')
3138
return chat

test_settings.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ def root(*args):
114114
SKILLS_IGNORED_THRESHOLD = 10
115115
SKILLS_IGNORED_RATIO_THRESHOLD = 0.8
116116

117-
CHAT_COMPLETION_API = 'http://test.chat.ai'
118-
CHAT_COMPLETION_API_KEY = 'test chat completion api key'
117+
CHAT_COMPLETION_API_V2 = 'http://test.chat.ai/v2'
118+
XPERT_CLIENT_ID = 'test client id'
119+
XPERT_DEFAULT_SYSTEM_MESSAGE = 'test system prompt'
119120

120121
JOB_DESCRIPTION_PROMPT = 'Generate a description for {job_name} job role.'
121122
JOB_TO_JOB_DESCRIPTION_PROMPT = 'How can a {current_job_name} switch to {future_job_name} job role.'

tests/openai/test_client.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestChatCompletionClient(TaxonomyTestCase):
1616
@responses.activate
1717
def test_client(self):
1818
"""
19-
Test that the chat completion client works as expected.
19+
Test that the chat completion client works as expected when an object is returned.
2020
"""
2121
chat_prompt = 'how many courses are offered by edx in the data science area'
2222
expected_chat_response = {
@@ -25,8 +25,26 @@ def test_client(self):
2525
}
2626
responses.add(
2727
method=responses.POST,
28-
url=settings.CHAT_COMPLETION_API,
28+
url=settings.CHAT_COMPLETION_API_V2,
2929
json=expected_chat_response,
3030
)
3131
chat_response = chat_completion(chat_prompt)
3232
self.assertEqual(chat_response, expected_chat_response['content'])
33+
34+
@responses.activate
35+
def test_client_with_list_response(self):
36+
"""
37+
Test that the chat completion client works as expected when a list is returned.
38+
"""
39+
chat_prompt = 'how many courses are offered by edx in the data science area'
40+
expected_chat_response = [{
41+
"role": "assistant",
42+
"content": "edx offers 500 courses in the data science area"
43+
}]
44+
responses.add(
45+
method=responses.POST,
46+
url=settings.CHAT_COMPLETION_API_V2,
47+
json=expected_chat_response,
48+
)
49+
chat_response = chat_completion(chat_prompt)
50+
self.assertEqual(chat_response, expected_chat_response[0]['content'])

0 commit comments

Comments
 (0)