-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for setting user-defined context variables #620
Comments
Just had a discussion about this issue with folks on our team. In the next few weeks, we'll probably add an initializer to /**
Create a `Context` object from an existing context and user-defined context variables.
- parameter context: An existing `Context` object.
- parameter variables: User-defined variables that should be included in this `Context` object.
*/
public init(context: Context, variables: [String: Any]? = nil) throws {
var json = context.json // get previous context
variables.forEach{ json[$0] = $1 } // add user-defined variables
Context(JSON(dictionary: json)) // initialize object
} This would keep the properties of the message(...) { response in
let context = Context(context: response.context, variables: ["topping": "onions"])
message(...) // send context with user-defined variables
} |
@bfaist also suggested an alternative approach, where the That brings up a few questions:
|
I like the solution mentioned above of adding the initializer that accepts an existing Context and merges custom variables. |
I opened #680 today with a number of updates to the Conversation service, including better support for getting/setting context variables. ExampleHere's an example of what it looks like. Curious to get your feedback, and whether you like/dislike it! // setting a context variable
let request = MessageRequest(...)
conversation.message(workspaceID: "your-workspace-id", request: request) {
response in
var context = response.context // `var` makes the context mutable
context?.additionalProperties["foo"] = .string("bar")
}
// getting a context variable
let request = MessageRequest(...)
conversation.message(workspaceID: "your-workspace-id", request: request) {
response in
if case let .string(bar) = additionalProperties["foo"]! {
print(bar)
}
} DetailsThe The /// A JSON value (one of string, number, object, array, true, false, or null).
public enum JSON: Equatable, Codable {
case null
case boolean(Bool)
case string(String)
case int(Int)
case double(Double)
case array([JSON])
case object([String: JSON])
} Instead of using an |
This was merged with #680. Waiting on a quick fix with Visual Recognition, then we'll publish an official release with these changes in a few days. |
These changes are included with release v0.19.0 of the Swift SDK. Hope that helps! |
Although the SDK supports (and includes readme examples for) accessing user-defined context variables, it does not provide good support for setting user-defined context variables.
For example, the
json
property is a constant (let
notvar
), preventing users from executingresponse.context?.json["foo"] = "bar"
.(I'm not sure whether this is the right way to expose user-defined context variables, but the example is instructive.)
In addition to an email that I received, other users in the community have struggled with this.
After designing a good mechanism for setting user-defined context variables, the readme should be updated with new examples to demonstrate it.
The text was updated successfully, but these errors were encountered: