Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/custom-config-ninja-api
Browse files Browse the repository at this point in the history
pamella committed Jun 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 1943544 + 3a62db6 commit 0fcd5b4
Showing 6 changed files with 515 additions and 8 deletions.
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
"""
Use this manage.py for generate the OpenAPI schema. NOT for examples.
Use this manage.py to generate the OpenAPI schema. NOT for examples.
For tests, use pytest directly.
"""
import os
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ pymdown-extensions = "^10.8.1"
markdown = "^3.6"
pygments = "^2.18.0"
model-bakery = "^1.18.1"
pytest-cov = "^5.0.0"

[tool.poetry.group.example.dependencies]
django-webpack-loader = "^3.1.0"
196 changes: 196 additions & 0 deletions tests/cassettes/test_views/test_create_thread_message.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
interactions:
- request:
body: '{"messages": [{"content": "You are a temperature bot. Today is 2024-06-09.",
"role": "system"}, {"content": "Hello, what is the temperature in SF right now?",
"role": "user"}], "model": "gpt-4o", "n": 1, "stream": true, "temperature":
1.0, "tools": [{"type": "function", "function": {"name": "fetch_current_temperature",
"description": "Fetch the current temperature data for a location", "parameters":
{"type": "object", "properties": {"location": {"type": "string"}}, "required":
["location"]}}}, {"type": "function", "function": {"name": "fetch_forecast_temperature",
"description": "Fetch the forecast temperature data for a location", "parameters":
{"type": "object", "properties": {"location": {"type": "string"}, "dt_str":
{"description": "Date in the format ''YYYY-MM-DD''", "type": "string"}}, "required":
["location", "dt_str"]}}}]}'
headers:
accept:
- application/json
accept-encoding:
- gzip, deflate
authorization:
- DUMMY
connection:
- keep-alive
content-length:
- '840'
content-type:
- application/json
host:
- api.openai.com
user-agent:
- OpenAI/Python
method: POST
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: 'data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_DSkKzAmCkD4pfwMPxeDZbXmN","type":"function","function":{"name":"fetch_current_temperature","arguments":""}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\""}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"location"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"San"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
Francisco"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":","}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
CA"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"}"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0n4JXzttxBAJOECR6Rcmb2ZrFe","object":"chat.completion.chunk","created":1718801253,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"tool_calls"}]}
data: [DONE]
'
headers:
Connection:
- keep-alive
Content-Type:
- text/event-stream; charset=utf-8
Date: Sun, 09 Jun 2024 23:39:08 GMT
Server: DUMMY
Transfer-Encoding:
- chunked
status:
code: 200
message: OK
- request:
body: '{"messages": [{"content": "You are a temperature bot. Today is 2024-06-09.",
"role": "system"}, {"content": "Hello, what is the temperature in SF right now?",
"role": "user"}, {"content": null, "role": "assistant", "tool_calls": [{"type":
"function", "id": "call_DSkKzAmCkD4pfwMPxeDZbXmN", "function": {"name": "fetch_current_temperature",
"arguments": "{\"location\": \"San Francisco, CA\"}"}}]}, {"content": "32 degrees
Celsius", "role": "tool", "tool_call_id": "call_DSkKzAmCkD4pfwMPxeDZbXmN"}],
"model": "gpt-4o", "n": 1, "stream": true, "temperature": 1.0, "tools": [{"type":
"function", "function": {"name": "fetch_current_temperature", "description":
"Fetch the current temperature data for a location", "parameters": {"type":
"object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}},
{"type": "function", "function": {"name": "fetch_forecast_temperature", "description":
"Fetch the forecast temperature data for a location", "parameters": {"type":
"object", "properties": {"location": {"type": "string"}, "dt_str": {"description":
"Date in the format ''YYYY-MM-DD''", "type": "string"}}, "required": ["location",
"dt_str"]}}}]}'
headers:
accept:
- application/json
accept-encoding:
- gzip, deflate
authorization:
- DUMMY
connection:
- keep-alive
content-length:
- '1162'
content-type:
- application/json
cookie:
- DUMMY
host:
- api.openai.com
user-agent:
- OpenAI/Python
method: POST
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: 'data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
current"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
temperature"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
in"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
San"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
Francisco"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":","},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
CA"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
is"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"32"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
degrees"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"
Celsius"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bp0oyb19UTjSTZGIZ9S9iH1Tedxm","object":"chat.completion.chunk","created":1718801254,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}
data: [DONE]
'
headers:
Connection:
- keep-alive
Content-Type:
- text/event-stream; charset=utf-8
Date: Sun, 09 Jun 2024 23:39:08 GMT
Server: DUMMY
Transfer-Encoding:
- chunked
status:
code: 200
message: OK
version: 1
196 changes: 196 additions & 0 deletions tests/cassettes/test_views/test_delete_thread_message.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
interactions:
- request:
body: '{"messages": [{"content": "You are a temperature bot. Today is 2024-06-09.",
"role": "system"}, {"content": "Hello, what is the temperature in SF right now?",
"role": "user"}], "model": "gpt-4o", "n": 1, "stream": true, "temperature":
1.0, "tools": [{"type": "function", "function": {"name": "fetch_current_temperature",
"description": "Fetch the current temperature data for a location", "parameters":
{"type": "object", "properties": {"location": {"type": "string"}}, "required":
["location"]}}}, {"type": "function", "function": {"name": "fetch_forecast_temperature",
"description": "Fetch the forecast temperature data for a location", "parameters":
{"type": "object", "properties": {"location": {"type": "string"}, "dt_str":
{"description": "Date in the format ''YYYY-MM-DD''", "type": "string"}}, "required":
["location", "dt_str"]}}}]}'
headers:
accept:
- application/json
accept-encoding:
- gzip, deflate
authorization:
- DUMMY
connection:
- keep-alive
content-length:
- '840'
content-type:
- application/json
host:
- api.openai.com
user-agent:
- OpenAI/Python
method: POST
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: 'data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_jZYnXesjvWYUOtcl9hJtkdGv","type":"function","function":{"name":"fetch_current_temperature","arguments":""}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\""}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"location"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"San"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
Francisco"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":","}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"
CA"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"}"}}]},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFYWsubgNdYM5o0AqjwHHOvD8KL","object":"chat.completion.chunk","created":1718802168,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_9cb5d38cf7","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"tool_calls"}]}
data: [DONE]
'
headers:
Connection:
- keep-alive
Content-Type:
- text/event-stream; charset=utf-8
Date: Sun, 09 Jun 2024 23:39:08 GMT
Server: DUMMY
Transfer-Encoding:
- chunked
status:
code: 200
message: OK
- request:
body: '{"messages": [{"content": "You are a temperature bot. Today is 2024-06-09.",
"role": "system"}, {"content": "Hello, what is the temperature in SF right now?",
"role": "user"}, {"content": null, "role": "assistant", "tool_calls": [{"type":
"function", "id": "call_jZYnXesjvWYUOtcl9hJtkdGv", "function": {"name": "fetch_current_temperature",
"arguments": "{\"location\": \"San Francisco, CA\"}"}}]}, {"content": "32 degrees
Celsius", "role": "tool", "tool_call_id": "call_jZYnXesjvWYUOtcl9hJtkdGv"}],
"model": "gpt-4o", "n": 1, "stream": true, "temperature": 1.0, "tools": [{"type":
"function", "function": {"name": "fetch_current_temperature", "description":
"Fetch the current temperature data for a location", "parameters": {"type":
"object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}},
{"type": "function", "function": {"name": "fetch_forecast_temperature", "description":
"Fetch the forecast temperature data for a location", "parameters": {"type":
"object", "properties": {"location": {"type": "string"}, "dt_str": {"description":
"Date in the format ''YYYY-MM-DD''", "type": "string"}}, "required": ["location",
"dt_str"]}}}]}'
headers:
accept:
- application/json
accept-encoding:
- gzip, deflate
authorization:
- DUMMY
connection:
- keep-alive
content-length:
- '1162'
content-type:
- application/json
cookie:
- DUMMY
host:
- api.openai.com
user-agent:
- OpenAI/Python
method: POST
uri: https://api.openai.com/v1/chat/completions
response:
body:
string: 'data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"The"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
current"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
temperature"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
in"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
San"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
Francisco"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":","},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
CA"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
is"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"32"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
degrees"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"
Celsius"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-9bpFZ4XRr8JskwOIpN1uE7TsBwgb0","object":"chat.completion.chunk","created":1718802169,"model":"gpt-4o-2024-05-13","system_fingerprint":"fp_f4e629d0a5","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}
data: [DONE]
'
headers:
Connection:
- keep-alive
Content-Type:
- text/event-stream; charset=utf-8
Date: Sun, 09 Jun 2024 23:39:08 GMT
Server: DUMMY
Transfer-Encoding:
- chunked
status:
code: 200
message: OK
version: 1
108 changes: 102 additions & 6 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -6,10 +6,12 @@
import pytest
from model_bakery import baker

from django_ai_assistant.exceptions import AIAssistantNotDefinedError
from django_ai_assistant import package_name, version
from django_ai_assistant.exceptions import AIAssistantNotDefinedError, AIUserNotAllowedError
from django_ai_assistant.helpers import use_cases
from django_ai_assistant.helpers.assistants import AIAssistant
from django_ai_assistant.langchain.tools import BaseModel, Field, method_tool
from django_ai_assistant.models import Thread
from django_ai_assistant.models import Message, Thread


# Set up
@@ -56,6 +58,18 @@ def authenticated_client(client):
return client


# OPENAPI JSON View


@pytest.mark.django_db()
def test_generates_json_with_correct_version(authenticated_client):
response = authenticated_client.get("/openapi.json")

assert response.status_code == HTTPStatus.OK
assert response.json()["info"]["version"] == version
assert response.json()["info"]["title"] == package_name


# Assistant Views


@@ -139,7 +153,7 @@ def test_gets_specific_thread(authenticated_client):
)

assert response.status_code == HTTPStatus.OK
assert response.json().get("id") == thread.id
assert response.json()["id"] == thread.id


def test_does_not_list_threads_if_unauthorized():
@@ -159,7 +173,7 @@ def test_create_thread(authenticated_client):
thread = Thread.objects.first()

assert response.status_code == HTTPStatus.OK
assert response.json().get("id") == thread.id
assert response.json()["id"] == thread.id


def test_cannot_create_thread_if_unauthorized():
@@ -231,6 +245,88 @@ def test_cannot_delete_thread_if_unauthorized():
pass


# Threads Messages Views (will need VCR)
# Threads Messages Views

# GET


@pytest.mark.django_db(transaction=True)
def test_list_thread_messages(authenticated_client):
thread = baker.make(Thread, created_by=User.objects.first())
use_cases.create_thread_message_as_user(thread.id, "Hello", thread.created_by)
response = authenticated_client.get(
reverse("django_ai_assistant:messages_list_create", kwargs={"thread_id": thread.id})
)

assert response.status_code == HTTPStatus.OK
assert len(response.json()) == 1


@pytest.mark.django_db(transaction=True)
def test_does_not_list_thread_messages_if_not_thread_user(authenticated_client):
with pytest.raises(AIUserNotAllowedError):
thread = baker.make(Thread)
use_cases.create_thread_message_as_user(thread.id, "Hello", User.objects.create())
authenticated_client.get(
reverse("django_ai_assistant:messages_list_create", kwargs={"thread_id": thread.id})
)


# POST


@pytest.mark.django_db(transaction=True)
@pytest.mark.vcr
def test_create_thread_message(authenticated_client):
thread = baker.make(Thread, created_by=User.objects.first())
response = authenticated_client.post(
reverse("django_ai_assistant:messages_list_create", kwargs={"thread_id": thread.id}),
data={
"content": "Hello, what is the temperature in SF right now?",
"assistant_id": "temperature_assistant",
},
content_type="application/json",
)

assert response.status_code == HTTPStatus.CREATED
assert Message.objects.count() == 2

human_message = Message.objects.filter(message__type="human").first()
ai_message = Message.objects.filter(message__type="ai").first()

assert (
human_message.message["data"]["content"]
== "Hello, what is the temperature in SF right now?"
)
assert (
ai_message.message["data"]["content"]
== "The current temperature in San Francisco, CA is 32 degrees Celsius."
)


# DELETE


@pytest.mark.django_db(transaction=True)
@pytest.mark.vcr
def test_delete_thread_message(authenticated_client):
thread = baker.make(Thread, created_by=User.objects.first())
authenticated_client.post(
reverse("django_ai_assistant:messages_list_create", kwargs={"thread_id": thread.id}),
data={
"content": "Hello, what is the temperature in SF right now?",
"assistant_id": "temperature_assistant",
},
content_type="application/json",
)
message = Message.objects.first()

# TBD
response = authenticated_client.delete(
reverse(
"django_ai_assistant:messages_delete",
kwargs={"thread_id": thread.id, "message_id": message.id},
)
)

assert response.status_code == HTTPStatus.NO_CONTENT
assert not Message.objects.filter(id=message.id).exists()

0 comments on commit 0fcd5b4

Please sign in to comment.