{"id":13288978,"url":"https://github.com/Azure-Samples/function-csharp-ai-openai-chatgpt","last_synced_at":"2025-03-10T06:33:52.955Z","repository":{"id":170603915,"uuid":"646709744","full_name":"Azure-Samples/function-csharp-ai-openai-chatgpt","owner":"Azure-Samples","description":"To gain access, please finish setting up this repository now at:  https://repos.opensource.microsoft.com/Azure-Samples/wizard?existingreponame=function-csharp-ai-openai-chatgpt\u0026existingrepoid=646709744","archived":false,"fork":false,"pushed_at":"2024-04-29T15:02:41.000Z","size":45,"stargazers_count":3,"open_issues_count":2,"forks_count":1,"subscribers_count":13,"default_branch":"main","last_synced_at":"2024-07-29T17:04:21.857Z","etag":null,"topics":["ai","azd-templates","azure-functions","chatgpt","csharp","openai"],"latest_commit_sha":null,"homepage":"https://repos.opensource.microsoft.com/Azure-Samples/wizard?existingreponame=function-csharp-ai-openai-chatgpt\u0026existingrepoid=646709744","language":"Bicep","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Azure-Samples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-29T06:56:47.000Z","updated_at":"2024-05-08T17:09:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"dc1605d6-b527-4e76-84b3-5f6939eadf84","html_url":"https://github.com/Azure-Samples/function-csharp-ai-openai-chatgpt","commit_stats":null,"previous_names":["azure-samples/function-csharp-ai-openai-chatgpt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-csharp-ai-openai-chatgpt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-csharp-ai-openai-chatgpt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-csharp-ai-openai-chatgpt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Azure-Samples%2Ffunction-csharp-ai-openai-chatgpt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Azure-Samples","download_url":"https://codeload.github.com/Azure-Samples/function-csharp-ai-openai-chatgpt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242805561,"owners_count":20187996,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","azd-templates","azure-functions","chatgpt","csharp","openai"],"created_at":"2024-07-29T17:00:17.718Z","updated_at":"2025-03-10T06:33:47.935Z","avatar_url":"https://github.com/Azure-Samples.png","language":"Bicep","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Azure Functions\n## Chat using ChatGPT (.NET C# v4 Isolated Function)\n\nThis sample shows how to take a ChatGPT prompt as HTTP Get or Post input, calculates the completions using OpenAI ChatGPT service, and then returns the output plus caches in a Blob state store.  \n\n[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://github.com/codespaces/new?hide_repo_select=true\u0026ref=main\u0026repo=575770869)\n\n## Run on your local environment\n\n### Pre-reqs\n1) [.NET 7 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) required *and [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) is strongly recommended*\n2) [Azure Functions Core Tools](https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=v4%2Cmacos%2Ccsharp%2Cportal%2Cbash#install-the-azure-functions-core-tools)\n``\n3) [OpenAPI API key](https://platform.openai.com/account/api-keys) \n4) Export these secrets as Env Vars using values from Step 3.\n\nMac/Linux\n```bash\nexport OPENAI_API_KEY=*Paste from step 3*\n```\n\nWindows\n\nSearch for Environment Variables in Settings, create new System Variables similarly to [these instructions](https://docs.oracle.com/en/database/oracle/machine-learning/oml4r/1.5.1/oread/creating-and-modifying-environment-variables-on-windows.html#GUID-DD6F9982-60D5-48F6-8270-A27EC53807D0):\n\n| Variable | Value |\n| -------- | ----- |\n| OPENAI_API_KEY | *Paste from step 3* |\n\n5) Add this local.settings.json file to the text_summarize folder to simplify local development and include Key from step 3\n```json\n{\n  \"IsEncrypted\": false,\n  \"Values\": {\n    \"AzureWebJobsStorage\": \"UseDevelopmentStorage=true\",\n    \"FUNCTIONS_WORKER_RUNTIME\": \"dotnet-isolated\",\n    \"OPENAI_API_KEY\": \"*Paste from step 3*\"\n  }\n}\n```\n\n### Using Functions CLI\n1) Open a new terminal and do the following:\n\n```bash\nfunc start\n```\n2) Using your favorite REST client, e.g. [RestClient in VS Code](https://marketplace.visualstudio.com/items?itemName=humao.rest-client), PostMan, curl, make a post.  `test.http` has been provided to run this quickly.   \n\nTerminal:\n```bash\ncurl -i -X POST http://localhost:7071/api/chat/ \\\n  -H \"Content-Type: text/json\" \\\n  --data-binary \"@testdata.json\"\n```\n\ntestdata.json\n```json\n{\n    \"prompt\": \"Write a poem about Azure Functions.  Include two reasons why users love them.\"\n}\n```\n\ntest.http\n```bash\n\nPOST http://localhost:7071/api/chat HTTP/1.1\ncontent-type: application/json\n\n{\n    \"prompt\": \"Write a poem about Azure Functions.  Include two reasons why users love them.\"\n}\n```\n\nYou will see chat happen in the Terminal standard out, the HTTP response, and saved off to a Blob for state management in the `samples-chatgpt-output` container.  \n\n### Using Visual Studio\n1) Open `chatgpt.sln` using Visual Studio 2022 or later.\n2) Press Run/F5 to run in the debugger\n3) Use same approach above to test using an HTTP REST client\n\n### Using Visual Studio Code\n1) Open this folder in a new terminal\n2) Open VS Code by entering `code .` in the terminal\n3) Press Run/Debug (F5) to run in the debugger\n4) Use same approach above to test using an HTTP REST client\n\n## Source Code\n\nThe key code that makes this work is as follows in `./chat_function.cs`.  C# uses an HTTP REST API wrapper function called `OpenAICreateCompletion` plus user defined Record types for convenience and type safety.  You can customize this or learn more snippets using [Examples](https://platform.openai.com/examples) and [OpenAPI Playground](https://platform.openai.com/playground/).  Choose CURL/HTTP as the language and set `CreateDefaultCompletionRequest` values accordingly.  \n\n```csharp\n// call OpenAI ChatGPT API here with desired chat prompt\nvar completion = await OpenAICreateCompletion(\"text-davinci-003\", GeneratePrompt(prompt), 0.9m, 64, logger);\n\nvar choice = completion.choices[0];\nlogger.LogInformation($\"Completions result: {choice}\");\n\nresponse = req.CreateResponse(HttpStatusCode.OK);\nawait response.WriteAsJsonAsync\u003cChoice\u003e(choice);\n\nreturn response;\n```\n\n## Deploy to Azure\n\nThe easiest way to deploy this app is using the [Azure Dev CLI aka AZD](https://aka.ms/azd).  If you open this repo in GitHub CodeSpaces the AZD tooling is already preinstalled.\n\nTo provision and deploy:\n```bash\nazd up\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Ffunction-csharp-ai-openai-chatgpt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAzure-Samples%2Ffunction-csharp-ai-openai-chatgpt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAzure-Samples%2Ffunction-csharp-ai-openai-chatgpt/lists"}