Skip to content

Commit

Permalink
Fixing user agent header name (#3545)
Browse files Browse the repository at this point in the history
# Description

Appending header values if clashing headers found between injected
headers and original headers in request.

# All Promptflow Contribution checklist:
- [ ] **The pull request does not introduce [breaking changes].**
- [ ] **CHANGELOG is updated for new features, bug fixes or other
significant changes.**
- [ ] **I have read the [contribution guidelines](../CONTRIBUTING.md).**
- [ ] **Create an issue and link to the pull request to get dedicated
review from promptflow team. Learn more: [suggested
workflow](../CONTRIBUTING.md#suggested-workflow).**

## General Guidelines and Best Practices
- [ ] Title of the pull request is clear and informative.
- [ ] There are a small number of commits, each of which have an
informative message. This means that previously merged commits do not
appear in the history of the PR. For more information on cleaning up the
commits in your PR, [see this
page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md).

### Testing Guidelines
- [ ] Pull request includes test coverage for the included changes.
  • Loading branch information
singankit authored Jul 16, 2024
1 parent 4f7f41f commit 696b129
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, model_config: AzureOpenAIModelConfiguration):
model_config.api_version = "2024-02-15-preview"

prompty_model_config = {"configuration": model_config}
prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-user-agent": USER_AGENT}}}) \
prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-useragent": USER_AGENT}}}) \
if USER_AGENT and isinstance(model_config, AzureOpenAIModelConfiguration) else None

current_dir = os.path.dirname(__file__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, model_config: AzureOpenAIModelConfiguration):
model_config.api_version = "2024-02-15-preview"

prompty_model_config = {"configuration": model_config}
prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-user-agent": USER_AGENT}}}) \
prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-useragent": USER_AGENT}}}) \
if USER_AGENT and isinstance(model_config, AzureOpenAIModelConfiguration) else None
current_dir = os.path.dirname(__file__)
prompty_path = os.path.join(current_dir, "fluency.prompty")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, model_config: AzureOpenAIModelConfiguration):

prompty_model_config = {"configuration": model_config}

prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-user-agent": USER_AGENT}}}) \
prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-useragent": USER_AGENT}}}) \
if USER_AGENT and isinstance(model_config, AzureOpenAIModelConfiguration) else None

current_dir = os.path.dirname(__file__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self, model_config: AzureOpenAIModelConfiguration):
"configuration": model_config,
}

prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-user-agent": USER_AGENT}}})\
prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-useragent": USER_AGENT}}})\
if USER_AGENT and isinstance(model_config, AzureOpenAIModelConfiguration) else None

current_dir = os.path.dirname(__file__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, model_config: AzureOpenAIModelConfiguration):
model_config.api_version = "2024-02-15-preview"

prompty_model_config = {"configuration": model_config}
prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-user-agent": USER_AGENT}}}) \
prompty_model_config.update({"parameters": {"extra_headers": {"x-ms-useragent": USER_AGENT}}}) \
if USER_AGENT and isinstance(model_config, AzureOpenAIModelConfiguration) else None
current_dir = os.path.dirname(__file__)
prompty_path = os.path.join(current_dir, "similarity.prompty")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ def inject_headers(kwargs):
injected_headers = get_aoai_telemetry_headers()
original_headers = kwargs.get("headers" if IS_LEGACY_OPENAI else "extra_headers")
if original_headers and isinstance(original_headers, dict):
injected_headers.update(original_headers)
for header in original_headers.keys():
if header in injected_headers:
# If the key already exists in injected_headers, concatenate the values with a space
injected_headers[header] = " ".join([injected_headers[header], original_headers[header]])
else:
# If the key does not exist in injected_headers, add it directly
injected_headers[header] = original_headers[header]

kwargs["headers" if IS_LEGACY_OPENAI else "extra_headers"] = injected_headers

if asyncio.iscoroutinefunction(f):
Expand Down
16 changes: 10 additions & 6 deletions src/promptflow-tracing/tests/unittests/test_openai_injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ def f(**kwargs):

if IS_LEGACY_OPENAI:
headers = "headers"
kwargs_1 = {"headers": {"a": 1, "b": 2}}
kwargs_1 = {"headers": {"a": 1, "b": 2, "x-ms-useragent": "user_agent_test"}}
kwargs_2 = {"headers": {"ms-azure-ai-promptflow-called-from": "aoai-tool"}}
else:
headers = "extra_headers"
kwargs_1 = {"extra_headers": {"a": 1, "b": 2}}
kwargs_1 = {"extra_headers": {"a": 1, "b": 2, "x-ms-useragent": "user_agent_test"}}
kwargs_2 = {"extra_headers": {"ms-azure-ai-promptflow-called-from": "aoai-tool"}}

injected_headers = get_aoai_telemetry_headers()
user_agent = injected_headers.get("x-ms-useragent", None)
assert user_agent is not None
assert f(a=1, b=2) == {"a": 1, "b": 2, headers: injected_headers}

merged_headers = {**injected_headers, "a": 1, "b": 2}
merged_headers = {**injected_headers, "a": 1, "b": 2, "x-ms-useragent": " ".join([user_agent, "user_agent_test"])}
assert f(**kwargs_1) == {headers: merged_headers}

aoai_tools_headers = injected_headers.copy()
Expand All @@ -70,17 +72,19 @@ async def f(**kwargs):

if IS_LEGACY_OPENAI:
headers = "headers"
kwargs_1 = {"headers": {"a": 1, "b": 2}}
kwargs_1 = {"headers": {"a": 1, "b": 2, "x-ms-useragent": "user_agent_test"}}
kwargs_2 = {"headers": {"ms-azure-ai-promptflow-called-from": "aoai-tool"}}
else:
headers = "extra_headers"
kwargs_1 = {"extra_headers": {"a": 1, "b": 2}}
kwargs_1 = {"extra_headers": {"a": 1, "b": 2, "x-ms-useragent": "user_agent_test"}}
kwargs_2 = {"extra_headers": {"ms-azure-ai-promptflow-called-from": "aoai-tool"}}

injected_headers = get_aoai_telemetry_headers()
user_agent = injected_headers.get("x-ms-useragent", None)
assert user_agent is not None
assert await f(a=1, b=2) == {"a": 1, "b": 2, headers: injected_headers}

merged_headers = {**injected_headers, "a": 1, "b": 2}
merged_headers = {**injected_headers, "a": 1, "b": 2, "x-ms-useragent": " ".join([user_agent, "user_agent_test"])}
assert await f(**kwargs_1) == {headers: merged_headers}

aoai_tools_headers = injected_headers.copy()
Expand Down

0 comments on commit 696b129

Please sign in to comment.