|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text; |
| 6 | +using Together.Clients; |
| 7 | +using Together.Models.Audio; |
| 8 | +using Together.Models.Batch; |
| 9 | +using Together.Models.CodeInterpreter; |
| 10 | +using Together.Models.Evaluations; |
| 11 | +using Together.Models.Videos; |
| 12 | + |
| 13 | +namespace Together.Tests.Clients; |
| 14 | + |
| 15 | +public class NewResourceClientTests : TestBase |
| 16 | +{ |
| 17 | + [Fact] |
| 18 | + public async Task BatchClient_CreateAsync_ReturnsJob() |
| 19 | + { |
| 20 | + var response = new HttpResponseMessage |
| 21 | + { |
| 22 | + StatusCode = HttpStatusCode.OK, |
| 23 | + Content = new StringContent( |
| 24 | + """{"job":{"id":"batch_1","input_file_id":"file_1","user_id":"user","file_size_bytes":10,"status":"IN_PROGRESS","job_deadline":"2024-01-01T00:00:00Z","created_at":"2024-01-01T00:00:00Z","endpoint":"/v1/completions","progress":0.5}}""") |
| 25 | + }; |
| 26 | + |
| 27 | + var client = new BatchClient(CreateMockHttpClient(response)); |
| 28 | + var result = await client.CreateAsync(new BatchCreateRequest { InputFileId = "file_1", Endpoint = "/v1/completions" }); |
| 29 | + |
| 30 | + Assert.Equal("batch_1", result.Id); |
| 31 | + Assert.Equal("file_1", result.InputFileId); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task EndpointClient_ListAsync_ParsesResponse() |
| 36 | + { |
| 37 | + var json = """{"data":[{"id":"ep1","object":"endpoint","name":"ep","model":"model","type":"dedicated","owner":"user","state":"STARTED","created_at":"2024-01-01T00:00:00Z"}]}"""; |
| 38 | + var response = new HttpResponseMessage |
| 39 | + { |
| 40 | + StatusCode = HttpStatusCode.OK, |
| 41 | + Content = new StringContent(json) |
| 42 | + }; |
| 43 | + |
| 44 | + var client = new EndpointClient(CreateMockHttpClient(response)); |
| 45 | + var results = await client.ListAsync(); |
| 46 | + |
| 47 | + Assert.Single(results); |
| 48 | + Assert.Equal("ep1", results[0].Id); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task HardwareClient_ListAsync_ReturnsHardware() |
| 53 | + { |
| 54 | + var json = """{"object":"list","data":[{"object":"hardware","id":"hw1","pricing":{"cents_per_minute":1},"specs":{"gpu_type":"A100","gpu_link":"NVLINK","gpu_memory":80,"gpu_count":1},"updated_at":"2024-01-01T00:00:00Z"}]}"""; |
| 55 | + var response = new HttpResponseMessage |
| 56 | + { |
| 57 | + StatusCode = HttpStatusCode.OK, |
| 58 | + Content = new StringContent(json) |
| 59 | + }; |
| 60 | + |
| 61 | + var client = new HardwareClient(CreateMockHttpClient(response)); |
| 62 | + var result = await client.ListAsync(); |
| 63 | + |
| 64 | + Assert.Single(result.Data); |
| 65 | + Assert.Equal("hw1", result.Data[0].Id); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public async Task JobClient_RetrieveAsync_ReturnsJob() |
| 70 | + { |
| 71 | + var json = """{"job_id":"job-1","args":{},"created_at":"2024-01-01","status":"Queued","status_updates":[],"type":"train","updated_at":"2024-01-01"}"""; |
| 72 | + var response = new HttpResponseMessage |
| 73 | + { |
| 74 | + StatusCode = HttpStatusCode.OK, |
| 75 | + Content = new StringContent(json) |
| 76 | + }; |
| 77 | + |
| 78 | + var client = new JobClient(CreateMockHttpClient(response)); |
| 79 | + var job = await client.RetrieveAsync("job-1"); |
| 80 | + |
| 81 | + Assert.Equal("job-1", job.JobId); |
| 82 | + Assert.Equal("Queued", job.Status); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task EvaluationClient_CreateAsync_ReturnsWorkflow() |
| 87 | + { |
| 88 | + var json = """{"workflow_id":"wf_1","status":"queued"}"""; |
| 89 | + var response = new HttpResponseMessage |
| 90 | + { |
| 91 | + StatusCode = HttpStatusCode.OK, |
| 92 | + Content = new StringContent(json) |
| 93 | + }; |
| 94 | + |
| 95 | + var client = new EvaluationClient(CreateMockHttpClient(response)); |
| 96 | + var result = await client.CreateAsync(new EvaluationCreateRequest |
| 97 | + { |
| 98 | + Type = "classify", |
| 99 | + Judge = new JudgeModelConfig { Model = "judge", ModelSource = "serverless", SystemTemplate = "template" }, |
| 100 | + InputDataFilePath = "file.jsonl", |
| 101 | + Labels = new List<string> { "yes" }, |
| 102 | + PassLabels = new List<string> { "yes" } |
| 103 | + }); |
| 104 | + |
| 105 | + Assert.Equal("wf_1", result.WorkflowId); |
| 106 | + Assert.Equal("queued", result.Status); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public async Task CodeInterpreterClient_RunAsync_ReturnsOutputs() |
| 111 | + { |
| 112 | + var json = """{"data":{"session_id":"sess","status":"completed","outputs":[{"type":"stdout","data":"hello"}]}}"""; |
| 113 | + var response = new HttpResponseMessage |
| 114 | + { |
| 115 | + StatusCode = HttpStatusCode.OK, |
| 116 | + Content = new StringContent(json) |
| 117 | + }; |
| 118 | + |
| 119 | + var client = new CodeInterpreterClient(CreateMockHttpClient(response)); |
| 120 | + var result = await client.RunAsync(new CodeInterpreterRequest { Code = "print('hi')" }); |
| 121 | + |
| 122 | + Assert.Equal("sess", result.Data.SessionId); |
| 123 | + Assert.Single(result.Data.Outputs); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public async Task AudioClient_CreateSpeechAsync_ReturnsBytes() |
| 128 | + { |
| 129 | + var audioBytes = new byte[] { 1, 2, 3 }; |
| 130 | + var response = new HttpResponseMessage |
| 131 | + { |
| 132 | + StatusCode = HttpStatusCode.OK, |
| 133 | + Content = new ByteArrayContent(audioBytes) |
| 134 | + }; |
| 135 | + response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("audio/wav"); |
| 136 | + |
| 137 | + var client = new AudioClient(CreateMockHttpClient(response)); |
| 138 | + var result = await client.CreateSpeechAsync(new AudioSpeechRequest { Model = "model", Input = "hello" }); |
| 139 | + |
| 140 | + Assert.NotNull(result.Data); |
| 141 | + Assert.Equal(audioBytes, result.Data); |
| 142 | + } |
| 143 | + |
| 144 | + [Fact] |
| 145 | + public async Task AudioClient_CreateSpeechAsync_ParsesStream() |
| 146 | + { |
| 147 | + var payload = "data: {\"b64\":\"AQI=\"}\n" + |
| 148 | + "data: [DONE]\n"; |
| 149 | + var response = new HttpResponseMessage |
| 150 | + { |
| 151 | + StatusCode = HttpStatusCode.OK, |
| 152 | + Content = new StringContent(payload) |
| 153 | + }; |
| 154 | + |
| 155 | + var client = new AudioClient(CreateMockHttpClient(response)); |
| 156 | + var request = new AudioSpeechRequest { Model = "model", Input = "hi", Stream = true }; |
| 157 | + var stream = await client.CreateSpeechAsync(request); |
| 158 | + |
| 159 | + var chunks = new List<byte[]>(); |
| 160 | + await foreach (var chunk in stream.Stream!) |
| 161 | + { |
| 162 | + chunks.Add(chunk); |
| 163 | + } |
| 164 | + |
| 165 | + Assert.Single(chunks); |
| 166 | + Assert.Equal(new byte[] { 1, 2 }, chunks[0]); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public async Task AudioClient_CreateTranscriptionAsync_ReturnsText() |
| 171 | + { |
| 172 | + var response = new HttpResponseMessage |
| 173 | + { |
| 174 | + StatusCode = HttpStatusCode.OK, |
| 175 | + Content = new StringContent("""{"text":"Hello"}""") |
| 176 | + }; |
| 177 | + |
| 178 | + var request = new AudioFileRequest |
| 179 | + { |
| 180 | + Content = new MemoryStream(Encoding.UTF8.GetBytes("data")), |
| 181 | + FileName = "audio.wav", |
| 182 | + Model = "whisper" |
| 183 | + }; |
| 184 | + |
| 185 | + var client = new AudioClient(CreateMockHttpClient(response)); |
| 186 | + var result = await client.CreateTranscriptionAsync(request); |
| 187 | + |
| 188 | + Assert.Equal("Hello", result.Response!.Text); |
| 189 | + } |
| 190 | + |
| 191 | + [Fact] |
| 192 | + public async Task VideoClient_CreateAsync_ReturnsId() |
| 193 | + { |
| 194 | + var response = new HttpResponseMessage |
| 195 | + { |
| 196 | + StatusCode = HttpStatusCode.OK, |
| 197 | + Content = new StringContent("""{"id":"video_1"}""") |
| 198 | + }; |
| 199 | + |
| 200 | + var client = new VideoClient(CreateMockHttpClient(response)); |
| 201 | + var result = await client.CreateAsync(new CreateVideoRequest { Model = "video-model" }); |
| 202 | + |
| 203 | + Assert.Equal("video_1", result.Id); |
| 204 | + } |
| 205 | +} |
0 commit comments