mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-02-07 11:05:05 +08:00
Fix: example code in session.py (#13004)
### What problem does this PR solve? Fix: example code in session.py #12950 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --------- Co-authored-by: Levi <stupse-tipp0j@icloud.com> Co-authored-by: writinwaters <93570324+writinwaters@users.noreply.github.com> Co-authored-by: Liu An <asiro@qq.com>
This commit is contained in:
@ -207,7 +207,12 @@ async def chat_completion_openai_like(tenant_id, chat_id):
|
|||||||
|
|
||||||
Alternatively, you can use Python's `OpenAI` client:
|
Alternatively, you can use Python's `OpenAI` client:
|
||||||
|
|
||||||
|
NOTE: Streaming via `client.chat.completions.create(stream=True, ...)` does
|
||||||
|
not return `reference` currently. The only way to return `reference` is
|
||||||
|
non-stream mode with `with_raw_response`.
|
||||||
|
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
import json
|
||||||
|
|
||||||
model = "model"
|
model = "model"
|
||||||
client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api/v1/chats_openai/<chat_id>")
|
client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api/v1/chats_openai/<chat_id>")
|
||||||
@ -215,15 +220,14 @@ async def chat_completion_openai_like(tenant_id, chat_id):
|
|||||||
stream = True
|
stream = True
|
||||||
reference = True
|
reference = True
|
||||||
|
|
||||||
completion = client.chat.completions.create(
|
request_kwargs = dict(
|
||||||
model=model,
|
model="model",
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "system", "content": "You are a helpful assistant."},
|
{"role": "system", "content": "You are a helpful assistant."},
|
||||||
{"role": "user", "content": "Who are you?"},
|
{"role": "user", "content": "Who are you?"},
|
||||||
{"role": "assistant", "content": "I am an AI assistant named..."},
|
{"role": "assistant", "content": "I am an AI assistant named..."},
|
||||||
{"role": "user", "content": "Can you tell me how to install neovim"},
|
{"role": "user", "content": "Can you tell me how to install neovim"},
|
||||||
],
|
],
|
||||||
stream=stream,
|
|
||||||
extra_body={
|
extra_body={
|
||||||
"reference": reference,
|
"reference": reference,
|
||||||
"reference_metadata": {
|
"reference_metadata": {
|
||||||
@ -240,19 +244,25 @@ async def chat_completion_openai_like(tenant_id, chat_id):
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
if stream:
|
if stream:
|
||||||
|
completion = client.chat.completions.create(stream=True, **request_kwargs)
|
||||||
for chunk in completion:
|
for chunk in completion:
|
||||||
print(chunk)
|
print(chunk)
|
||||||
if reference and chunk.choices[0].finish_reason == "stop":
|
|
||||||
print(f"Reference:\n{chunk.choices[0].delta.reference}")
|
|
||||||
print(f"Final content:\n{chunk.choices[0].delta.final_content}")
|
|
||||||
else:
|
else:
|
||||||
print(completion.choices[0].message.content)
|
resp = client.chat.completions.with_raw_response.create(
|
||||||
if reference:
|
stream=False, **request_kwargs
|
||||||
print(completion.choices[0].message.reference)
|
)
|
||||||
|
print("status:", resp.http_response.status_code)
|
||||||
|
raw_text = resp.http_response.text
|
||||||
|
print("raw:", raw_text)
|
||||||
|
|
||||||
|
data = json.loads(raw_text)
|
||||||
|
print("assistant:", data["choices"][0]["message"].get("content"))
|
||||||
|
print("reference:", data["choices"][0]["message"].get("reference"))
|
||||||
|
|
||||||
"""
|
"""
|
||||||
req = await get_request_json()
|
req = await get_request_json()
|
||||||
|
|
||||||
|
|||||||
@ -65,8 +65,17 @@ Whether to receive the response as a stream. Set this to `false` explicitly if y
|
|||||||
|
|
||||||
#### Examples
|
#### Examples
|
||||||
|
|
||||||
|
> **Note**
|
||||||
|
> Streaming via `client.chat.completions.create(stream=True, ...)` does not
|
||||||
|
> return `reference` currently because `reference` is only exposed in the
|
||||||
|
> non-stream response payload. The only way to return `reference` is non-stream
|
||||||
|
> mode with `with_raw_response`.
|
||||||
|
:::caution NOTE
|
||||||
|
Streaming via `client.chat.completions.create(stream=True, ...)` does not return `reference` because it is *only* included in the raw response payload in non-stream mode. To return `reference`, set `stream=False`.
|
||||||
|
:::
|
||||||
```python
|
```python
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
import json
|
||||||
|
|
||||||
model = "model"
|
model = "model"
|
||||||
client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api/v1/chats_openai/<chat_id>")
|
client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api/v1/chats_openai/<chat_id>")
|
||||||
@ -74,7 +83,7 @@ client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api
|
|||||||
stream = True
|
stream = True
|
||||||
reference = True
|
reference = True
|
||||||
|
|
||||||
completion = client.chat.completions.create(
|
request_kwargs = dict(
|
||||||
model=model,
|
model=model,
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "system", "content": "You are a helpful assistant."},
|
{"role": "system", "content": "You are a helpful assistant."},
|
||||||
@ -82,26 +91,32 @@ completion = client.chat.completions.create(
|
|||||||
{"role": "assistant", "content": "I am an AI assistant named..."},
|
{"role": "assistant", "content": "I am an AI assistant named..."},
|
||||||
{"role": "user", "content": "Can you tell me how to install neovim"},
|
{"role": "user", "content": "Can you tell me how to install neovim"},
|
||||||
],
|
],
|
||||||
stream=stream,
|
|
||||||
extra_body={
|
extra_body={
|
||||||
|
"extra_body": {
|
||||||
"reference": reference,
|
"reference": reference,
|
||||||
"reference_metadata": {
|
"reference_metadata": {
|
||||||
"include": True,
|
"include": True,
|
||||||
"fields": ["author", "year", "source"],
|
"fields": ["author", "year", "source"],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
if stream:
|
if stream:
|
||||||
|
completion = client.chat.completions.create(stream=True, **request_kwargs)
|
||||||
for chunk in completion:
|
for chunk in completion:
|
||||||
print(chunk)
|
print(chunk)
|
||||||
if reference and chunk.choices[0].finish_reason == "stop":
|
|
||||||
print(f"Reference:\n{chunk.choices[0].delta.reference}")
|
|
||||||
print(f"Final content:\n{chunk.choices[0].delta.final_content}")
|
|
||||||
else:
|
else:
|
||||||
print(completion.choices[0].message.content)
|
resp = client.chat.completions.with_raw_response.create(
|
||||||
if reference:
|
stream=False, **request_kwargs
|
||||||
print(completion.choices[0].message.reference)
|
)
|
||||||
|
print("status:", resp.http_response.status_code)
|
||||||
|
raw_text = resp.http_response.text
|
||||||
|
print("raw:", raw_text)
|
||||||
|
|
||||||
|
data = json.loads(raw_text)
|
||||||
|
print("assistant:", data["choices"][0]["message"].get("content"))
|
||||||
|
print("reference:", data["choices"][0]["message"].get("reference"))
|
||||||
```
|
```
|
||||||
|
|
||||||
When `extra_body.reference_metadata.include` is `true`, each reference chunk may include a `document_metadata` object in both streaming and non-streaming responses.
|
When `extra_body.reference_metadata.include` is `true`, each reference chunk may include a `document_metadata` object in both streaming and non-streaming responses.
|
||||||
|
|||||||
Reference in New Issue
Block a user