Last active
October 10, 2024 14:34
-
-
Save houstonhaynes/da7345092da56d9e18ee90ad60efed9c to your computer and use it in GitHub Desktop.
Function that calls Azure OpenAI endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let callAzureOpenAI (text: string) = | |
task { | |
let apiKey = Environment.GetEnvironmentVariable("AZUREOPENAI_API_KEY") | |
if String.IsNullOrEmpty(apiKey) then | |
return! ServerError.failwith (ServerError.Exception "Azure OpenAI API key is not set.") | |
else | |
let url = "https://[YOUR_AZURE_SERVICE]/openai/deployments/gpt-35-turbo/chat/completions?api-version=2024-02-15-preview" | |
use httpClient = new HttpClient() | |
httpClient.DefaultRequestHeaders.Add("api-key", apiKey) | |
let requestPayload = { | |
messages = [ | |
{ role = "system"; content = "Extract the following information from the text and respond with a JSON object with ONLY the following keys: name, email, title, phone, address1, address2, city, state, zip, country. For each key, infer a value from inputs. Only return one value for each key. For fields without any corresponding information in inputs, use the value null." } | |
{ role = "user"; content = text } | |
] | |
max_tokens = 800 | |
temperature = 0.7 | |
frequency_penalty = 0.0 | |
presence_penalty = 0.0 | |
top_p = 0.95 | |
stop = None | |
} | |
let content = new StringContent(JsonConvert.SerializeObject(requestPayload), System.Text.Encoding.UTF8, "application/json") | |
let! response = httpClient.PostAsync(url, content) | |
let! responseBody = response.Content.ReadAsStringAsync() | |
if response.StatusCode = HttpStatusCode.OK then | |
parseAndPrintMessage responseBody | |
return responseBody | |
else | |
return! ServerError.failwith (ServerError.Exception $"Failed to call Azure OpenAI API: {response.StatusCode} - {responseBody}") | |
} | |
|> Async.AwaitTask |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment