Skip to content

Commit bad8bd0

Browse files
chore: cleanup and refactor core client (#146)
* ci: no need to use yapf as we have pylint * chore: remove MANIFEST.in since we are not using setuptools * chore: move all files into module * chore: update project * docs: added instrutions on how to use just the core client * chore: try limiting python runs to only the changed package * Revert "chore: try limiting python runs to only the changed package" This reverts commit e93b664. * chore: add python core specific validation action * chore: add build step for smoke test
1 parent 8eedc6a commit bad8bd0

File tree

20 files changed

+101
-60
lines changed

20 files changed

+101
-60
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Build and validate Python core package
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
branches: ["main"]
7+
paths:
8+
- "python/packages/microsoft_agents_m365copilot_core/microsoft_agents_m365copilot_core/**"
9+
- "python/packages/microsoft_agents_m365copilot_core/tests/**"
10+
- "python/packages/microsoft_agents_m365copilot_core/pyproject.toml"
11+
12+
permissions:
13+
contents: read
14+
actions: read
15+
security-events: write
16+
17+
jobs:
18+
build-and-test-python-core:
19+
name: python-microsoft_agents_m365copilot_core
20+
runs-on: ubuntu-latest
21+
strategy:
22+
max-parallel: 5
23+
matrix:
24+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
25+
steps:
26+
- uses: actions/checkout@v4
27+
- name: Set up Python ${{ matrix.python-version }}
28+
uses: actions/setup-python@v5
29+
with:
30+
python-version: ${{ matrix.python-version }}
31+
32+
- name: Install dependencies
33+
run: |
34+
python -m pip install --upgrade poetry
35+
poetry install
36+
working-directory: python/packages/microsoft_agents_m365copilot_core
37+
38+
- name: Build
39+
run: |
40+
poetry build
41+
working-directory: python/packages/microsoft_agents_m365copilot_core
42+
43+
- name: Check import order
44+
run: |
45+
poetry run isort .
46+
working-directory: python/packages/microsoft_agents_m365copilot_core
47+
48+
- name: Run tests
49+
run: |
50+
poetry run pytest .
51+
working-directory: python/packages/microsoft_agents_m365copilot_core
52+
53+
# Common CodeQL steps
54+
- name: Initialize CodeQL
55+
uses: github/codeql-action/init@v3
56+
with:
57+
languages: python
58+
- name: Autobuild
59+
uses: github/codeql-action/autobuild@v3
60+
- name: Perform CodeQL Analysis
61+
uses: github/codeql-action/analyze@v3
62+
with:
63+
category: "/language:python"

.github/workflows/build-and-test.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ jobs:
140140
working-directory: ${{ matrix.packages.path }}
141141

142142
# Core package specific steps
143-
- name: Check code format
144-
if: matrix.packages.name == 'microsoft_agents_m365copilot_core'
145-
run: |
146-
poetry run yapf -dr .
147-
working-directory: ${{ matrix.packages.path }}
148-
149143
- name: Check import order
150144
if: matrix.packages.name == 'microsoft_agents_m365copilot_core'
151145
run: |

python/packages/microsoft_agents_m365copilot_core/MANIFEST.in

Lines changed: 0 additions & 12 deletions
This file was deleted.

python/packages/microsoft_agents_m365copilot_core/README.md

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,18 @@ CLIENT_ID = "YOUR_CLIENT_ID"
6161

6262
> **Note:**
6363
>
64-
> This example shows how to make a call to the Microsoft 365 Copilot Retrieval API. To call this API, you need to install the [Microsoft 365 Copilot APIs Python Beta Client Library](https://github.com/microsoft/Agents-M365Copilot/tree/main/python/packages/microsoft_agents_m365copilot_beta), create a request object and then run the POST method on the request.
64+
> This example shows how to make a call to the Microsoft 365 Copilot Retrieval API using just the core library. Alternately, you can use the [Microsoft 365 Copilot APIs Python Beta Client Library](https://github.com/microsoft/Agents-M365Copilot/tree/main/python/packages/microsoft_agents_m365copilot_beta) to create a request object and then run the POST method on the request.
6565
6666
```python
6767
import asyncio
6868
import os
6969
from datetime import datetime
7070

7171
from azure.identity import DeviceCodeCredential
72-
from dotenv import load_dotenv
73-
from kiota_abstractions.api_error import APIError
72+
from dotenv import load_dotenv
7473

75-
from microsoft_agents_m365copilot_beta import AgentsM365CopilotBetaServiceClient
76-
from microsoft_agents_m365copilot_beta.generated.copilot.retrieval.retrieval_post_request_body import (
77-
RetrievalPostRequestBody,
78-
)
79-
from microsoft_agents_m365copilot_beta.generated.models.retrieval_data_source import RetrievalDataSource
74+
from microsoft_agents_m365copilot_core.src._enums import APIVersion
75+
from microsoft_agents_m365copilot_core.src.client_factory import MicrosoftAgentsM365CopilotClientFactory
8076

8177
load_dotenv()
8278

@@ -96,44 +92,39 @@ credentials = DeviceCodeCredential(
9692
prompt_callback=auth_callback
9793
)
9894

99-
# Use the Graph API beta endpoint explicitly
100-
scopes = ['https://graph.microsoft.com/.default']
101-
client = AgentsM365CopilotBetaServiceClient(credentials=credentials, scopes=scopes)
95+
client = MicrosoftAgentsM365CopilotClientFactory.create_with_default_middleware(api_version=APIVersion.beta)
10296

103-
# Make sure the base URL is set to beta
104-
client.request_adapter.base_url = "https://graph.microsoft.com/beta"
97+
client.base_url = "https://graph.microsoft.com/beta" # Make sure the base URL is set to beta
10598

10699
async def retrieve():
107100
try:
108-
# Print the URL being used
109-
print(f"Using API base URL: {client.request_adapter.base_url}\n")
110-
111-
# Create the retrieval request body
112-
retrieval_body = RetrievalPostRequestBody()
113-
retrieval_body.data_source = RetrievalDataSource.SharePoint
114-
retrieval_body.query_string = "What is the latest in my organization?"
115-
116-
# Try more parameters that might be required
117-
# retrieval_body.maximum_number_of_results = 10
101+
# Kick off device code flow and get the token.
102+
loop = asyncio.get_running_loop()
103+
token = await loop.run_in_executor(None, lambda: credentials.get_token("https://graph.microsoft.com/.default"))
104+
105+
# Set the access token.
106+
headers = {"Authorization": f"Bearer {token.token}"}
107+
108+
# Print the URL being used.
109+
print(f"Using API base URL for incoming request: {client.base_url}\n")
110+
111+
# Directly use httpx to test the endpoint.
112+
response = await client.post("https://graph.microsoft.com/beta/copilot/retrieval", json={
113+
"queryString": "What is the latest in my organization?",
114+
"dataSource": "sharePoint",
115+
"resourceMetadata": [
116+
"title",
117+
"author"
118+
],
119+
"maximumNumberOfResults": "10"
120+
}, headers=headers)
121+
122+
# Show the response
123+
print(f"Response HTTP status: {response.status_code}")
124+
print(f"Response JSON content: {response.text}")
118125

119-
# Make the API call
120-
print("Making retrieval API request...")
121-
retrieval = await client.copilot.retrieval.post(retrieval_body)
122-
123-
# Process the results
124-
if retrieval and hasattr(retrieval, "retrieval_hits"):
125-
print(f"Received {len(retrieval.retrieval_hits)} hits")
126-
for r in retrieval.retrieval_hits:
127-
print(f"Web URL: {r.web_url}\n")
128-
for extract in r.extracts:
129-
print(f"Text:\n{extract.text}\n")
130-
else:
131-
print(f"Retrieval response structure: {dir(retrieval)}")
132-
except APIError as e:
133-
print(f"Error: {e.error.code}: {e.error.message}")
134-
if hasattr(e, 'error') and hasattr(e.error, 'inner_error'):
135-
print(f"Inner error details: {e.error.inner_error}")
136-
raise e
126+
finally:
127+
print("Your call to the Copilot APIs is now complete.")
137128

138129
# Run the async function
139130
asyncio.run(retrieve())

0 commit comments

Comments
 (0)