Skip to content

Commit a50bf6d

Browse files
[azopenai] Adding in a contributors guide, some documentation to explain how bits fit together. (Azure#21609)
Fixes Azure#21560
1 parent 0cda95c commit a50bf6d

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

sdk/ai/azopenai/CONTRIBUTING.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Contributing Guide
2+
3+
> NOTE: these instructions are for fixing or adding features to the `azopenai` module. To use the module refer to the readme for this package: [readme.md](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/ai/azopenai/README.md).
4+
5+
This is a contributing guide for the `azopenai` package. For general contributing guidelines refer to [CONTRIBUTING.md](https://github.com/Azure/azure-sdk-for-go/blob/main/CONTRIBUTING.md).
6+
7+
The `azopenai` package can be used with either Azure OpenAI or OpenAI's public service. New features are added using our code generation process, specified using TypeSpec [TypeSpec](https://github.com/Microsoft/typespec), which details all the models and protocol methods for using OpenAI.
8+
9+
### Prerequisites
10+
11+
For code fixes that do not require code generation:
12+
- Go 1.18 (or greater)
13+
14+
For code generation:
15+
- [NodeJS (use the latest LTS)](https://nodejs.org)
16+
- [TypeSpec compiler](https://github.com/Microsoft/typespec#getting-started).
17+
- [autorest](https://github.com/Azure/autorest/tree/main/packages/apps/autorest)
18+
- [PowerShell Core](https://github.com/PowerShell/PowerShell#get-powershell)
19+
- [goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports)
20+
21+
# Building
22+
23+
## Generating from TypeSpec
24+
25+
The `Client` is primarily generated from TypeSpec, with some handwritten code where we've changed the interface to match Azure naming conventions (for instance, we refer to Models as Deployments). Files that do not have `custom` (ex: `client.go`, `models.go`, `models_serde.go`, etc..) are generated.
26+
27+
Files that have `custom` in the name are handwritten (ex: `custom_client_audio.go`), while files that do not (ex: `client.go`, `models.go`, `models_serde.go`, etc..) are generated.
28+
29+
### Regeneration
30+
31+
The `testdata/tsp-location.yaml` specifies the specific revision (and repo) that we use to generate the client. This also makes it possible, if needed, to generate from branch commmits in [`Azure/azure-rest-api-specs`](https://github.com/Azure/azure-rest-api-specs).
32+
33+
**tsp.location.yaml**:
34+
```yaml
35+
# ie: https://github.com/Azure/azure-rest-api-specs/tree/1e243e2b0d0d006599dcb64f82fd92aecc1247be/specification/cognitiveservices/OpenAI.Inference
36+
directory: specification/cognitiveservices/OpenAI.Inference
37+
commit: 1e243e2b0d0d006599dcb64f82fd92aecc1247be
38+
repo: Azure/azure-rest-api-specs
39+
```
40+
The generation process is all done as `go generate` commands in `build.go`. To regenerate the client run:
41+
42+
```
43+
go generate ./...
44+
```
45+
46+
Commit the generated changes as part of your pull request.
47+
48+
If the changes don't look quite right you can adjust the generated code using the `autorest.md` file.
49+
50+
# Testing
51+
52+
There are three kinds of tests for this package: unit tests, recorded tests and live tests.
53+
54+
## Unit and recorded tests
55+
56+
Unit tests and recorded tests do not require access to OpenAI to run and will run with any PR as a check-in gate.
57+
58+
Recorded tests require the Azure SDK test proxy is running. See the instructions for [installing the test-proxy](https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md#installation).
59+
60+
In one terminal window, start the test-proxy:
61+
62+
```bash
63+
cd <root of the azopenai module>
64+
test-proxy
65+
```
66+
67+
In another terminal window:
68+
69+
70+
To playback (ie: use recordings):
71+
```bash
72+
cd <root of the azopenai module>
73+
74+
export AZURE_RECORD_MODE=playback
75+
go test -count 1 -v ./...
76+
```
77+
78+
To re-record:
79+
```bash
80+
cd <root of the azopenai module>
81+
82+
export AZURE_RECORD_MODE=record
83+
go test -count 1 -v ./...
84+
85+
# push the recording changes to the repo
86+
test-proxy push -a assets.json
87+
88+
# commit our assets.json file now that it points
89+
# to the new recordings.
90+
git add assets.json
91+
git commit -m "updated recordings"
92+
git push
93+
```
94+
95+
## Live tests
96+
97+
### Local development
98+
99+
Copy the `sample.env` file to `.env`, and fill out all the values. Each value is documented to give you a general idea of what's needed, but ultimately you'll need to work with the Azure OpenAI SDK team to figure out which services are used for which features.
100+
101+
Once filled out, the tests will automatically load environment variables from the `.env`:
102+
103+
```bash
104+
export AZURE_RECORD_MODE=live
105+
go test -count 1 -v ./...
106+
```
107+
108+
### Pull requests
109+
110+
Post a comment to your PR with this text:
111+
112+
```
113+
/azp run go - azopenai
114+
```
115+
116+
The build bot will post a comment indicating its started the pipeline and the checks will start showing up in the status for the PR as well.

sdk/ai/azopenai/client_shared_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type endpoint struct {
3838
Azure bool
3939
}
4040

41+
// newTestClient creates a client enabled for HTTP recording, if needed.
42+
// See [newRecordingTransporter] for sanitization code.
4143
func newTestClient(t *testing.T, ep endpoint) *azopenai.Client {
4244
if ep.Azure {
4345
cred, err := azopenai.NewKeyCredential(ep.APIKey)
@@ -55,13 +57,14 @@ func newTestClient(t *testing.T, ep endpoint) *azopenai.Client {
5557
cred, err := azopenai.NewKeyCredential(ep.APIKey)
5658
require.NoError(t, err)
5759

58-
// we get rate limited quite a bit.
5960
options := newClientOptionsForTest(t)
6061

6162
if options == nil {
6263
options = &azopenai.ClientOptions{}
6364
}
6465

66+
// We get rate limited quite a bit with OpenAI so we use this _very_ forgiving retry
67+
// policy to make our tests pass consistently.
6568
options.Retry = policy.RetryOptions{
6669
MaxRetries: 60,
6770
RetryDelay: time.Second,
@@ -127,6 +130,8 @@ const fakeCognitiveIndexName = "index"
127130

128131
func initEnvVars() {
129132
if recording.GetRecordMode() == recording.PlaybackMode {
133+
// Setup our variables so our requests are consistent with what we recorded.
134+
// Endpoints are sanitized using the recording policy
130135
azureOpenAI.Endpoint = endpoint{
131136
URL: fakeEndpoint,
132137
APIKey: fakeAPIKey,
@@ -197,6 +202,8 @@ func initEnvVars() {
197202
}
198203
}
199204

205+
// newRecordingTransporter sets up our recording policy to sanitize endpoints and any parts of the response that might
206+
// involve UUIDs that would make the response/request inconsistent.
200207
func newRecordingTransporter(t *testing.T) policy.Transporter {
201208
transport, err := recording.NewRecordingHTTPClient(t, nil)
202209
require.NoError(t, err)
@@ -253,6 +260,11 @@ func newRecordingTransporter(t *testing.T) policy.Transporter {
253260
return transport
254261
}
255262

263+
// newClientOptionsForTest creates options that enable a few optional things:
264+
// - If we're recording then it injects the recording transporter
265+
// - If `SSLKEYLOGFILE` is set in the environment it'll automatically setup
266+
// the HTTP policy so it writes the keylog for that client to a file. You can
267+
// use this with WireShark to decrypt and view a network trace.
256268
func newClientOptionsForTest(t *testing.T) *azopenai.ClientOptions {
257269
co := &azopenai.ClientOptions{}
258270

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#location: https://github.com/Azure/azure-rest-api-specs/tree/1393b6e34d7370733e3e2236c4df686280a96f36/specification/cognitiveservices/OpenAI.Inference
22
directory: specification/cognitiveservices/OpenAI.Inference
3-
#commit: 90600bad43a463e874ab3bee0064302373933d4a
43
commit: 1e243e2b0d0d006599dcb64f82fd92aecc1247be
54
repo: Azure/azure-rest-api-specs

0 commit comments

Comments
 (0)