Azure-Samples/azure-search-openai-demo でログを有効にする

# TL;DR

logging 使いましょうという話

```python:app.py
# 全体を ERROR レベルにして stdout に出す
logging.basicConfig(stream=sys.stdout, level=ERROR)
# 自分のアプリのロギング用ロガー
logger = getLogger("app")
logger.setLevel(INFO)

# OpenAI SDK の出力
openaiLogger = getLogger("openai")
openaiLogger.setLevel(DEBUG)
```

# はじめに

[Azure Open AI Service](https://azure.microsoft.com/ja-jp/products/cognitive-services/openai-service/) をサクッと試したい時のサンプルとして以下のリポジトリがあります。

https://github.com/Azure-Samples/azure-search-openai-demo

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4914/cd6f27fa-29f6-6548-4a24-7a59412427ca.png)


このリポジトリは、App Service をユーザーインターフェイスとして、AOAI と Cognitive Service を利用したアプリケーションとなっています。詳細はリポジトリを参照してください。

App Service には python/quart で実装されたアプリケーションがデプロイされます。

# デプロイしてみる

アプリをデプロイしてポチポチ操作してみると、以下のようなエラーが発生することがあります。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4914/991f6618-194f-ed08-01ca-fcc3ee308ce2.png)

エラーメッセージそのまま Rate limit にひっかかったようです。

リポジトリの README にあるように、`AZURE_USE_APPLICATION_INSIGHTS`　オプションを指定してプロビジョニングすることで、Application Insights からトランザクションの診断を見ることが可能です。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/4914/208f3591-1345-d26a-8006-f485e109e416.png)

https://learn.microsoft.com/ja-jp/azure/azure-monitor/app/transaction-diagnostics

一方でアプリケーションのログとしては、以下の内容のようにほとんど有益な情報が得られません。

```log
2023-08-30T04:23:14.319209855Z    _____                               
2023-08-30T04:23:14.319251455Z   /  _  \ __________ _________   ____  
2023-08-30T04:23:14.319255555Z  /  /_\  \\___   /  |  \_  __ \_/ __ \ 
2023-08-30T04:23:14.319258756Z /    |    \/    /|  |  /|  | \/\  ___/ 
2023-08-30T04:23:14.319261856Z \____|__  /_____ \____/ |__|    \___  >
2023-08-30T04:23:14.319265256Z         \/      \/                  \/ 
2023-08-30T04:23:14.319268156Z A P P   S E R V I C E   O N   L I N U X
2023-08-30T04:23:14.319271056Z 
2023-08-30T04:23:14.319273756Z Documentation: http://aka.ms/webapp-linux
2023-08-30T04:23:14.319280756Z Python 3.10.12
2023-08-30T04:23:14.319283656Z Note: Any data outside '/home' is not persisted
2023-08-30T04:23:22.361535787Z Starting OpenBSD Secure Shell server: sshd.
2023-08-30T04:23:22.602325870Z Site's appCommandLine: python3 -m gunicorn main:app
2023-08-30T04:23:23.075950243Z Starting periodic command scheduler: cron.
2023-08-30T04:23:23.094186654Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -userStartupCommand 'python3 -m gunicorn main:app'
2023-08-30T04:23:23.274168034Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2023-08-30T04:23:23.285807968Z Output is compressed. Extracting it...
2023-08-30T04:23:23.285845569Z Build Operation ID: 973a4580c5ea413e
2023-08-30T04:23:23.285850569Z Oryx Version: 0.2.20230707.1, Commit: 0bd28e69919b5e8beba451e8677e3345f0be8361, ReleaseTagName: 20230707.1
2023-08-30T04:23:23.344187043Z Extracting '/home/site/wwwroot/output.tar.gz' to directory '/tmp/8dba91007344254'...
2023-08-30T04:23:42.572598625Z App path is set to '/tmp/8dba91007344254'
2023-08-30T04:23:42.733833920Z Writing output script to '/opt/startup/startup.sh'
2023-08-30T04:23:43.069081767Z Using packages from virtual environment antenv located at /tmp/8dba91007344254/antenv.
2023-08-30T04:23:43.069114567Z Updated PYTHONPATH to '/opt/startup/app_logs:/tmp/8dba91007344254/antenv/lib/python3.10/site-packages'
2023-08-30T04:23:48.098615066Z [2023-08-30 04:23:48 +0000] [72] [INFO] Starting gunicorn 20.1.0
2023-08-30T04:23:48.107738397Z [2023-08-30 04:23:48 +0000] [72] [INFO] Listening at: http://0.0.0.0:8000 (72)
2023-08-30T04:23:48.108713811Z [2023-08-30 04:23:48 +0000] [72] [INFO] Using worker: uvicorn.workers.UvicornWorker
2023-08-30T04:23:48.128981502Z [2023-08-30 04:23:48 +0000] [73] [INFO] Booting worker with pid: 73
2023-08-30T04:23:48.246053480Z [2023-08-30 04:23:48 +0000] [74] [INFO] Booting worker with pid: 74
2023-08-30T04:23:48.327558849Z [2023-08-30 04:23:48 +0000] [75] [INFO] Booting worker with pid: 75
2023-08-30T04:25:18.555309230Z [2023-08-30 04:25:18 +0000] [75] [INFO] Started server process [75]
2023-08-30T04:25:18.556978551Z [2023-08-30 04:25:18 +0000] [75] [INFO] Waiting for application startup.
2023-08-30T04:25:18.644641169Z [2023-08-30 04:25:18 +0000] [74] [INFO] Started server process [74]
2023-08-30T04:25:18.676539576Z [2023-08-30 04:25:18 +0000] [73] [INFO] Started server process [73]
2023-08-30T04:25:18.676546176Z [2023-08-30 04:25:18 +0000] [74] [INFO] Waiting for application startup.
2023-08-30T04:25:18.676549776Z [2023-08-30 04:25:18 +0000] [73] [INFO] Waiting for application startup.
2023-08-30T04:25:27.901349336Z [2023-08-30 04:25:27 +0000] [74] [INFO] Application startup complete.
2023-08-30T04:25:27.901681740Z [2023-08-30 04:25:27 +0000] [75] [INFO] Application startup complete.
2023-08-30T04:25:27.901690140Z [2023-08-30 04:25:27 +0000] [73] [INFO] Application startup complete.
```

## ログ出力を追加

App Service にデプロイされるアプリケーションのメインのロジックは [`azure-search-openai-demo/app/backend
/app.py`](https://github.com/Azure-Samples/azure-search-openai-demo/blob/main/app/backend/app.py#L68-L104) となります。

ここにログを追加していきます。

以下のように logger から標準出力に出るようにします。
`openai SDK`からの出力は、`openai` という名前のロガーが利用されているようなので、こちらもいったん `DEBUG` レベルにしておきます。

```python:app.py
# 全体を ERROR レベルにして stdout に出す
logging.basicConfig(stream=sys.stdout, level=ERROR)
# 自分のアプリのロギング用ロガー
logger = getLogger("app")
logger.setLevel(INFO)

# OpenAI SDK の出力
openaiLogger = getLogger("openai")
openaiLogger.setLevel(DEBUG)
```
https://github.com/openai/openai-python/blob/main/openai/util.py#L12

あとはお好みで、ログを出したいか所に `logger.info` などで出力しましょう。

#### 例
```python:app.py
@bp.route("/chat", methods=["POST"])
async def chat():
    if not request.is_json:
        return jsonify({"error": "request must be json"}), 415
    request_json = await request.get_json()
    approach = request_json["approach"]
    logger.info("================= /chat Request =================\n")
    logger.info(request_json)
    try:
        impl = current_app.config[CONFIG_CHAT_APPROACHES].get(approach)
        if not impl:
            return jsonify({"error": "unknown approach"}), 400
        # Workaround for: https://github.com/openai/openai-python/issues/371
        async with aiohttp.ClientSession() as s:
            openai.aiosession.set(s)
            r = await impl.run(request_json["history"], request_json.get("overrides") or {})
        logger.info("================= /chat Response =================\n")
        logger.info(r)
        return jsonify(r)
    except Exception as e:
        logging.exception("Exception in /chat")
        return jsonify({"error": str(e)}), 500
```

### ログ

#### 正常系

```log
2023-08-30T07:30:10.812298358Z INFO:app:================= /chat Request =================
2023-08-30T07:30:10.813050268Z 
2023-08-30T07:30:10.813057268Z INFO:app:{'history': [{'user': 'What happens in a performance review?', 'bot': "During a performance review, employees will have a discussion with their supervisor about their performance over the past year. The supervisor will provide feedback on areas for improvement and discuss goals and objectives for the upcoming year. The review is a two-way dialogue, and employees are encouraged to be honest and open during the process. A written summary of the performance review will be provided, including a rating of the employee's performance, feedback, and goals and objectives [employee_handbook-3.pdf]."}, {'user': 'What happens in a performance review?'}], 'approach': 'rrr', 'overrides': {'retrieval_mode': 'hybrid', 'semantic_ranker': True, 'semantic_captions': False, 'top': 3, 'suggest_followup_questions': False}}
2023-08-30T07:30:14.226988586Z DEBUG:openai:message='Request to OpenAI API' method=post path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/chat/chat/completions?api-version=2023-05-15
2023-08-30T07:30:14.228145701Z DEBUG:openai:api_version=2023-05-15 data='{"model": "gpt-35-turbo", "messages": [{"role": "system", "content": "Below is a history of the conversation so far, and a new question asked by the user that needs to be answered by searching in a knowledge base about employee healthcare plans and the employee handbook.\\nGenerate a search query based on the conversation and the new question.\\nDo not include cited source filenames and document names e.g info.txt or doc.pdf in the search query terms.\\nDo not include any text inside [] or <<>> in the search query terms.\\nDo not include any special characters like \'+\'.\\nIf the question is not in English, translate the question to English before generating the search query.\\nIf you cannot generate a search query, return just the number 0.\\n"}, {"role": "assistant", "content": "Health plan cardio coverage"}, {"role": "user", "content": "does my plan cover cardio?"}, {"role": "assistant", "content": "Show available health plans"}, {"role": "user", "content": "What are my health plans?"}, {"role": "user", "content": "What happens in a performance review?"}, {"role": "assistant", "content": "During a performance review, employees will have a discussion with their supervisor about their performance over the past year. The supervisor will provide feedback on areas for improvement and discuss goals and objectives for the upcoming year. The review is a two-way dialogue, and employees are encouraged to be honest and open during the process. A written summary of the performance review will be provided, including a rating of the employee\'s performance, feedback, and goals and objectives [employee_handbook-3.pdf]."}, {"role": "user", "content": "Generate search query for: What happens in a performance review?"}], "temperature": 0.0, "max_tokens": 32, "n": 1}' message='Post details'
2023-08-30T07:30:14.651612588Z INFO:openai:message='OpenAI API response' path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/chat/chat/completions?api-version=2023-05-15 processing_ms=314.4287 request_id=c043fac5-59ff-40e3-80c5-3d95eca9c688 response_code=200
2023-08-30T07:30:14.664341546Z DEBUG:openai:message='Request to OpenAI API' method=post path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/embedding/embeddings?api-version=2023-05-15
2023-08-30T07:30:14.665351259Z DEBUG:openai:api_version=2023-05-15 data='{"input": "performance review process steps", "encoding_format": "base64"}' message='Post details'
2023-08-30T07:30:14.745549960Z INFO:openai:message='OpenAI API response' path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/embedding/embeddings?api-version=2023-05-15 processing_ms=73.3315 request_id=f5f42094-758b-49fe-8a56-a812351bd422 response_code=200

2023-08-30T07:30:15.977352740Z DEBUG:openai:message='Request to OpenAI API' method=post path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/chat/chat/completions?api-version=2023-05-15
2023-08-30T07:30:15.986760157Z DEBUG:openai:api_version=2023-05-15 data='{"model": "gpt-35-turbo", "messages": [{"role": "system", "content": "Assistant helps the company employees with their healthcare plan questions, and questions about the employee handbook. Be brief in your answers.\\nAnswer ONLY with the facts listed in the list of sources below. If there isn\'t enough information below, say you don\'t know. Do not generate answers that don\'t use the sources below. If asking a clarifying question to the user would help, ask the question.\\nFor tabular information return it as an html table. Do not return markdown format. If the question is not in English, answer in the language used in the question.\\nEach source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brackets to reference the source, e.g. [info1.txt]. Don\'t combine sources, list each source separately, e.g. [info1.txt][info2.pdf].\\n\\n\\n"}, {"role": "user", "content": "What happens in a performance review?"}, {"role": "assistant", "content": "During a performance review, employees will have a discussion with their supervisor about their performance over the past year. The supervisor will provide feedback on areas for improvement and discuss goals and objectives for the upcoming year. The review is a two-way dialogue, and employees are encouraged to be honest and open during the process. A written summary of the performance review will be provided, including a rating of the employee\'s performance, feedback, and goals and objectives [employee_handbook-3.pdf]."}, {"role": "user", "content": "What happens in a performance review?\\n\\nSources:\\nemployee_handbook-3.pdf:  Accountability: We take responsibility for our actions and hold ourselves and others accountable for their performance. 8. Community: We are committed to making a positive impact in the communities in which we work and live. Performance Reviews Performance Reviews at Contoso Electronics At Contoso Electronics, we strive to ensure our employees are getting the feedback they need to continue growing and developing in their roles. We understand that performance reviews are a key part of this process and it is important to us that they are conducted in an effective and efficient manner. Performance reviews are conducted annually and are an important part of your career development. During the review, your supervisor will discuss your performance over the past year and provide feedback on areas for improvement. They will also provide you with an opportunity to discuss your goals and objectives for the upcoming year. Performance reviews are a two-way dialogue between managers and employees. We encourage all employees to be honest and open during the review process, as it is an important opportunity to \\nemployee_handbook-3.pdf:  We encourage all employees to be honest and open during the review process, as it is an important opportunity to discuss successes and challenges in the workplace. We aim to provide positive and constructive feedback during performance reviews. This feedback should be used as an opportunity to help employees develop and grow in their roles. Employees will receive a written summary of their performance review which will be discussed during the review session. This written summary will include a rating of the employee\\u2019s performance, feedback, and goals and objectives for the upcoming year. We understand that performance reviews can be a stressful process. We are committed to making sure that all employees feel supported and empowered during the process. We encourage all employees to reach out to their managers with any questions or concerns they may have. We look forward to conducting performance reviews with all our employees. They are an important part of our commitment to helping our employees grow and develop in their roles.\\nrole_library-14.pdf: appropriate strategies \\u2022 Foster a culture of engagement, diversity, and inclusion \\u2022 Lead HR team to provide coaching and guidance to all employees \\u2022 Manage performance review process and identify areas for improvement \\u2022 Provide guidance and support to managers on disciplinary action \\u2022 Maintain employee records and manage payroll QUALIFICATIONS: \\u2022 Bachelor\\u2019s degree in Human Resources, Business Administration, or related field \\u2022 At least 8 years of experience in Human Resources, including at least 5 years in a managerial role \\u2022 Knowledgeable in Human Resources principles, theories, and practices \\u2022 Excellent communication and interpersonal skills \\u2022 Ability to lead, motivate, and develop a high-performing HR team \\u2022 Strong analytical and problem-solving skills \\u2022 Ability to handle sensitive information with discretion \\u2022 Proficient in Microsoft Office Suite Director of Research and Development Job Title: Director of Research and Development, Contoso Electronics Position Summary: The Director of Research and Development is a critical leadership role in Contoso Electronics. This position is responsible for leading the research, development and innovation of our products and services."}], "temperature": 0.7, "max_tokens": 1024, "n": 1}' message='Post details'
2023-08-30T07:30:17.248584269Z INFO:openai:message='OpenAI API response' path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/chat/chat/completions?api-version=2023-05-15 processing_ms=1236.4786 request_id=8c6b1d18-4bdf-4229-9736-fea21e0e8243 response_code=200
2023-08-30T07:30:17.248628869Z INFO:app:================= /chat Response =================
2023-08-30T07:30:17.248633169Z 
2023-08-30T07:30:17.251625007Z INFO:app:{'data_points': ['employee_handbook-3.pdf:  Accountability: We take responsibility for our actions and hold ourselves and others accountable for their performance. 8. Community: We are committed to making a positive impact in the communities in which we work and live. Performance Reviews Performance Reviews at Contoso Electronics At Contoso Electronics, we strive to ensure our employees are getting the feedback they need to continue growing and developing in their roles. We understand that performance reviews are a key part of this process and it is important to us that they are conducted in an effective and efficient manner. Performance reviews are conducted annually and are an important part of your career development. During the review, your supervisor will discuss your performance over the past year and provide feedback on areas for improvement. They will also provide you with an opportunity to discuss your goals and objectives for the upcoming year. Performance reviews are a two-way dialogue between managers and employees. We encourage all employees to be honest and open during the review process, as it is an important opportunity to ', 'employee_handbook-3.pdf:  We encourage all employees to be honest and open during the review process, as it is an important opportunity to discuss successes and challenges in the workplace. We aim to provide positive and constructive feedback during performance reviews. This feedback should be used as an opportunity to help employees develop and grow in their roles. Employees will receive a written summary of their performance review which will be discussed during the review session. This written summary will include a rating of the employee’s performance, feedback, and goals and objectives for the upcoming year. We understand that performance reviews can be a stressful process. We are committed to making sure that all employees feel supported and empowered during the process. We encourage all employees to reach out to their managers with any questions or concerns they may have. We look forward to conducting performance reviews with all our employees. They are an important part of our commitment to helping our employees grow and develop in their roles.', 'role_library-14.pdf: appropriate strategies • Foster a culture of engagement, diversity, and inclusion • Lead HR team to provide coaching and guidance to all employees • Manage performance review process and identify areas for improvement • Provide guidance and support to managers on disciplinary action • Maintain employee records and manage payroll QUALIFICATIONS: • Bachelor’s degree in Human Resources, Business Administration, or related field • At least 8 years of experience in Human Resources, including at least 5 years in a managerial role • Knowledgeable in Human Resources principles, theories, and practices • Excellent communication and interpersonal skills • Ability to lead, motivate, and develop a high-performing HR team • Strong analytical and problem-solving skills • Ability to handle sensitive information with discretion • Proficient in Microsoft Office Suite Director of Research and Development Job Title: Director of Research and Development, Contoso Electronics Position Summary: The Director of Research and Development is a critical leadership role in Contoso Electronics. This position is responsible for leading the research, development and innovation of our products and services.'], 'answer': "During a performance review at Contoso Electronics, employees will have a discussion with their supervisor about their performance over the past year. The supervisor will provide feedback on areas for improvement and discuss goals and objectives for the upcoming year. The review is a two-way dialogue, and employees are encouraged to be honest and open during the process. A written summary of the performance review will be provided, including a rating of the employee's performance, feedback, and goals and objectives [employee_handbook-3.pdf].", 'thoughts': 'Searched for:<br>performance review process steps<br><br>Conversations:<br>{\'role\': \'system\', \'content\': "Assistant helps the company employees with their healthcare plan questions, and questions about the employee handbook. Be brief in your answers.\\nAnswer ONLY with the facts listed in the list of sources below. If there isn\'t enough information below, say you don\'t know. Do not generate answers that don\'t use the sources below. If asking a clarifying question to the user would help, ask the question.\\nFor tabular information return it as an html table. Do not return markdown format. If the question is not in English, answer in the language used in the question.\\nEach source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brackets to reference the source, e.g. [info1.txt]. Don\'t combine sources, list each source separately, e.g. [info1.txt][info2.pdf].\\n\\n\\n"}<br><br>{\'role\': \'user\', \'content\': \'What happens in a performance review?\'}<br><br>{\'role\': \'assistant\', \'content\': "During a performance review, employees will have a discussion with their supervisor about their performance over the past year. The supervisor will provide feedback on areas for improvement and discuss goals and objectives for the upcoming year. The review is a two-way dialogue, and employees are encouraged to be honest and open during the process. A written summary of the performance review will be provided, including a rating of the employee\'s performance, feedback, and goals and objectives [employee_handbook-3.pdf]."}<br><br>{\'role\': \'user\', \'content\': \'What happens in a performance review?\\n\\nSources:\\nemployee_handbook-3.pdf:  Accountability: We take responsibility for our actions and hold ourselves and others accountable for their performance. 8. Community: We are committed to making a positive impact in the communities in which we work and live. Performance Reviews Performance Reviews at Contoso Electronics At Contoso Electronics, we strive to ensure our employees are getting the feedback they need to continue growing and developing in their roles. We understand that performance reviews are a key part of this process and it is important to us that they are conducted in an effective and efficient manner. Performance reviews are conducted annually and are an important part of your career development. During the review, your supervisor will discuss your performance over the past year and provide feedback on areas for improvement. They will also provide you with an opportunity to discuss your goals and objectives for the upcoming year. Performance reviews are a two-way dialogue between managers and employees. We encourage all employees to be honest and open during the review process, as it is an important opportunity to \\nemployee_handbook-3.pdf:  We encourage all employees to be honest and open during the review process, as it is an important opportunity to discuss successes and challenges in the workplace. We aim to provide positive and constructive feedback during performance reviews. This feedback should be used as an opportunity to help employees develop and grow in their roles. Employees will receive a written summary of their performance review which will be discussed during the review session. This written summary will include a rating of the employee’s performance, feedback, and goals and objectives for the upcoming year. We understand that performance reviews can be a stressful process. We are committed to making sure that all employees feel supported and empowered during the process. We encourage all employees to reach out to their managers with any questions or concerns they may have. We look forward to conducting performance reviews with all our employees. They are an important part of our commitment to helping our employees grow and develop in their roles.\\nrole_library-14.pdf: appropriate strategies • Foster a culture of engagement, diversity, and inclusion • Lead HR team to provide coaching and guidance to all employees • Manage performance review process and identify areas for improvement • Provide guidance and support to managers on disciplinary action • Maintain employee records and manage payroll QUALIFICATIONS: • Bachelor’s degree in Human Resources, Business Administration, or related field • At least 8 years of experience in Human Resources, including at least 5 years in a managerial role • Knowledgeable in Human Resources principles, theories, and practices • Excellent communication and interpersonal skills • Ability to lead, motivate, and develop a high-performing HR team • Strong analytical and problem-solving skills • Ability to handle sensitive information with discretion • Proficient in Microsoft Office Suite Director of Research and Development Job Title: Director of Research and Development, Contoso Electronics Position Summary: The Director of Research and Development is a critical leadership role in Contoso Electronics. This position is responsible for leading the research, development and innovation of our products and services.\'}'}

```

### 異常系

```log
2023-08-30T07:11:06.183590700Z INFO:app:================= /chat Request =================
2023-08-30T07:11:06.183628500Z 
2023-08-30T07:11:06.185107519Z INFO:app:{'history': [{'user': 'What happens in a performance review?', 'bot': "During a performance review, employees will have a discussion with their supervisor about their performance over the past year. The supervisor will provide feedback on areas for improvement and discuss goals and objectives for the upcoming year. The review is a two-way dialogue, and employees are encouraged to be honest and open during the process. A written summary of the performance review will be provided, including a rating of the employee's performance, feedback, and goals and objectives [employee_handbook-3.pdf]."}, {'user': 'What happens in a performance review?'}], 'approach': 'rrr', 'overrides': {'retrieval_mode': 'hybrid', 'semantic_ranker': True, 'semantic_captions': False, 'top': 3, 'suggest_followup_questions': False}}
2023-08-30T07:11:06.194096832Z DEBUG:openai:message='Request to OpenAI API' method=post path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/chat/chat/completions?api-version=2023-05-15
2023-08-30T07:11:06.195056944Z DEBUG:openai:api_version=2023-05-15 data='{"model": "gpt-35-turbo", "messages": [{"role": "system", "content": "Below is a history of the conversation so far, and a new question asked by the user that needs to be answered by searching in a knowledge base about employee healthcare plans and the employee handbook.\\nGenerate a search query based on the conversation and the new question.\\nDo not include cited source filenames and document names e.g info.txt or doc.pdf in the search query terms.\\nDo not include any text inside [] or <<>> in the search query terms.\\nDo not include any special characters like \'+\'.\\nIf the question is not in English, translate the question to English before generating the search query.\\nIf you cannot generate a search query, return just the number 0.\\n"}, {"role": "assistant", "content": "Health plan cardio coverage"}, {"role": "user", "content": "does my plan cover cardio?"}, {"role": "assistant", "content": "Show available health plans"}, {"role": "user", "content": "What are my health plans?"}, {"role": "user", "content": "What happens in a performance review?"}, {"role": "assistant", "content": "During a performance review, employees will have a discussion with their supervisor about their performance over the past year. The supervisor will provide feedback on areas for improvement and discuss goals and objectives for the upcoming year. The review is a two-way dialogue, and employees are encouraged to be honest and open during the process. A written summary of the performance review will be provided, including a rating of the employee\'s performance, feedback, and goals and objectives [employee_handbook-3.pdf]."}, {"role": "user", "content": "Generate search query for: What happens in a performance review?"}], "temperature": 0.0, "max_tokens": 32, "n": 1}' message='Post details'
2023-08-30T07:11:06.405620800Z INFO:openai:message='OpenAI API response' path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/chat/chat/completions?api-version=2023-05-15 processing_ms=179.1313 request_id=ff683688-cfd6-4bb6-bbcf-1d0b3e49ef31 response_code=200
2023-08-30T07:11:06.407177420Z DEBUG:openai:message='Request to OpenAI API' method=post path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/embedding/embeddings?api-version=2023-05-15
2023-08-30T07:11:06.408086531Z DEBUG:openai:api_version=2023-05-15 data='{"input": "performance review process steps", "encoding_format": "base64"}' message='Post details'
2023-08-30T07:11:06.427306174Z INFO:openai:message='OpenAI API response' path=https://cog-jr2grvghqa5bs.openai.azure.com/openai/deployments/embedding/embeddings?api-version=2023-05-15 processing_ms=None request_id=None response_code=429
2023-08-30T07:11:06.428375487Z INFO:openai:error_code=429 error_message='Rate limit is exceeded. Try again in 2 seconds.' error_param=None error_type=None message='OpenAI API error received' stream_error=False
2023-08-30T07:11:06.433057346Z ERROR:root:Exception in /chat
2023-08-30T07:11:06.433076447Z Traceback (most recent call last):
2023-08-30T07:11:06.433081347Z   File "/tmp/8dba926da8b2f85/app.py", line 118, in chat
2023-08-30T07:11:06.433084947Z     r = await impl.run(request_json["history"], request_json.get("overrides") or {})
2023-08-30T07:11:06.433143348Z   File "/tmp/8dba926da8b2f85/approaches/chatreadretrieveread.py", line 96, in run
2023-08-30T07:11:06.433148548Z     query_vector = (await openai.Embedding.acreate(engine=self.embedding_deployment, input=query_text))["data"][0]["embedding"]
2023-08-30T07:11:06.433151948Z   File "/tmp/8dba926da8b2f85/antenv/lib/python3.10/site-packages/openai/api_resources/embedding.py", line 73, in acreate
2023-08-30T07:11:06.433155448Z     response = await super().acreate(*args, **kwargs)
2023-08-30T07:11:06.433158748Z   File "/tmp/8dba926da8b2f85/antenv/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 217, in acreate
2023-08-30T07:11:06.433162148Z     response, _, api_key = await requestor.arequest(
2023-08-30T07:11:06.433165248Z   File "/tmp/8dba926da8b2f85/antenv/lib/python3.10/site-packages/openai/api_requestor.py", line 382, in arequest
2023-08-30T07:11:06.433168548Z     resp, got_stream = await self._interpret_async_response(result, stream)
2023-08-30T07:11:06.433171648Z   File "/tmp/8dba926da8b2f85/antenv/lib/python3.10/site-packages/openai/api_requestor.py", line 726, in _interpret_async_response
2023-08-30T07:11:06.433174948Z     self._interpret_response_line(
2023-08-30T07:11:06.433177948Z   File "/tmp/8dba926da8b2f85/antenv/lib/python3.10/site-packages/openai/api_requestor.py", line 763, in _interpret_response_line
2023-08-30T07:11:06.433181148Z     raise self.handle_error_response(
2023-08-30T07:11:06.433184348Z openai.error.RateLimitError: Rate limit is exceeded. Try again in 2 seconds.
```

リクエストID などがわかりますね。

## 参考

App Service on Linux のログ出力について

https://jpazpaas.github.io/blog/2023/07/27/AppServiceOnLinux-Log.html

Python のロギングについて

https://docs.python.org/ja/3/howto/logging.html#logging-howto