Azure Samples での 応答、新旧比較

# 背景

openai.demo の Front-Back でのやりとりが新しくなっていたのでそのメモ

# 概要

以下、サンプルにおける、response の新旧比較

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

旧側は、日本語サンプルである以下を基準とした。

https://github.com/Azure-Samples/jp-azureopenai-samples

# 比較

## 新

### backend

backend 側にテストが用意されているので、その辺りを確認するのが手っ取り早い

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

ちなみに、正常時の応答の一例が以下

```json: response sample
{
    "context": {
        "data_points": {
            "text": [
                "Benefit_Options-2.pdf: There is a whistleblower policy."
            ]
        },
        "thoughts": [
            {
                "description": "What is the capital of France?",
                "props": {
                    "filter": null,
                    "top": 3,
                    "use_semantic_captions": false,
                    "use_semantic_ranker": false,
                    "use_text_search": true,
                    "use_vector_search": true
                },
                "title": "Search using user query"
            },
            {
                "description": [
                    {
                        "captions": [
                            {
                                "additional_properties": {},
                                "highlights": [],
                                "text": "Caption: A whistleblower policy."
                            }
                        ],
                        "category": null,
                        "content": "There is a whistleblower policy.",
                        "embedding": null,
                        "groups": null,
                        "id": "file-Benefit_Options_pdf-42656E656669745F4F7074696F6E732E706466-page-2",
                        "imageEmbedding": null,
                        "oids": null,
                        "reranker_score": 3.4577205181121826,
                        "score": 0.03279569745063782,
                        "sourcefile": "Benefit_Options.pdf",
                        "sourcepage": "Benefit_Options-2.pdf"
                    }
                ],
                "props": null,
                "title": "Search results"
            },
            {
                "description": [
                    "{'role': 'system', 'content': \"You are an intelligent assistant helping Contoso Inc employees with their healthcare plan questions and employee handbook questions. Use 'you' to refer to the individual asking the questions even if they ask with 'I'. Answer the following question using only the data provided in the sources below. For tabular information return it as an html table. Do not return markdown format. Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. If you cannot answer using the sources below, say you don't know. Use below example to answer\"}",
                    "{'role': 'user', 'content': \"\\n'What is the deductible for the employee plan for a visit to Overlake in Bellevue?'\\n\\nSources:\\ninfo1.txt: deductibles depend on whether you are in-network or out-of-network. In-network deductibles are $500 for employee and $1000 for family. Out-of-network deductibles are $1000 for employee and $2000 for family.\\ninfo2.pdf: Overlake is in-network for the employee plan.\\ninfo3.pdf: Overlake is the name of the area that includes a park and ride near Bellevue.\\ninfo4.pdf: In-network institutions include Overlake, Swedish and others in the region\\n\"}",
                    "{'role': 'assistant', 'content': 'In-network deductibles are $500 for employee and $1000 for family [info1.txt] and Overlake is in-network for the employee plan [info2.pdf][info4.pdf].'}",
                    "{'role': 'user', 'content': 'What is the capital of France?\\nSources:\\n Benefit_Options-2.pdf: There is a whistleblower policy.'}"
                ],
                "props": {
                    "model": "gpt-35-turbo"
                },
                "title": "Prompt to generate answer"
            }
        ]
    },
    "message": {
        "content": "The capital of France is Paris. [Benefit_Options-2.pdf].",
        "function_call": null,
        "role": "assistant",
        "tool_calls": null
    },
    "session_state": null
}
```

https://github.com/Azure-Samples/azure-search-openai-demo/blob/main/tests/snapshots/test_app/test_ask_rtr_hybrid/client0/result.json

### frontend

front 側の定義は、以下を見るのが良い

https://github.com/Azure-Samples/azure-search-openai-demo/blob/main/app/frontend/src/api/models.ts#L64

## 旧

### backend

この時期のサンプルには test が用意されていなかった為、日本語版デモでも tests が無い。
その為、新のように、result.json は無いので、実際の応答生成部分を見るしかない

とはいえ、この頃は simple なので特に困らない

```python: 応答例
return {
    "data_points": results,
    "answer": response_text, 
    "thoughts": f"Searched for:<br>{query_text}<br><br>Conversations:<br>" + msg_to_display.replace('\n', '<br>')
    }
```

https://github.com/Azure-Samples/jp-azureopenai-samples/blob/main/5.internal-document-search/src/backend/approaches/chatreadretrieveread.py#L149

### front

新と同様に、models.ts を確認すればよく、ここで比較してみるのも良い

https://github.com/Azure-Samples/jp-azureopenai-samples/blob/main/5.internal-document-search/src/frontend/src/api/models.ts#L26

# あとがき

以下の影響で差分が大きくなってるのが困るところ

- Stream 対応による、"delta"
- ThoughtStep() による、"thoughts" の視覚強化。まぁ、ここは無視でも :sweat: