steamship-core / steamship-langchain

steamship-langchain
MIT License
513 stars 95 forks source link

Integrate with a SwiftUI app? #13

Closed alelordelo closed 1 year ago

alelordelo commented 1 year ago

Hello team,

Any example or tutorial on how I could integrate steamship langchain with a SwiftUI app?

douglas-reid commented 1 year ago

@alelordelo we don't have a tutorial on SwiftUI integrations. we have examples of integration with Vercel frontends, but that's about it. however, if you build using Steamship (in general, not limited to just steamship-langchain), you can deploy your package and that will get you a hosted API endpoint. You could then invoke that endpoint (with the proper credentials) from inside your SwiftUI app using standard tooling for making HTTP calls. I'm not a SwiftUI person at all, but it looks like there are guides on how to do that part on the web (https://designcode.io/swiftui-advanced-handbook-http-request).

does that sound like what you are looking for? if so, I can provide more involved details on deployment and access.

alelordelo commented 1 year ago

Hi Douglas,

Thanks for the tutorial!

Yup, HTTP access could work. Are all LangChain APIs available from Streamlit when deployed? Could you please send an example of a LangChain deployed app, with API endpoints?

douglas-reid commented 1 year ago

@alelordelo (fyi: i edited your reply to strip out your personal details. i hope that is ok).

When deployed (via ship it), you will have access to whatever you have packaged in your application. Steamship does not impose any specific limitations on the content of those apps -- outside of a general size limitation on the total generated package. That means that you can import langchain and have things work as expected (even if you don't want to use steamship-langchain, though we highly-recommend it).

If you look at our README.md in this repo, you should see links to several example packages built with Steamship and LangChain that can be deployed. There is also a template for a chatbot with a frontend available here. @EniasCailliau has also built a nice "Ask My Book" template (https://www.steamship.com/build/ask-my-book-site) that showcases some of this functionality.

I'll follow-up in my next comment with details on obtaining the precise API endpoint address and what you need to do for sending auth creds for when you are not using our TypeScript or Python clients.

douglas-reid commented 1 year ago

@alelordelo here is a pointer to calling HTTP directly from our docs: https://docs.steamship.com/packages/using.html#can-i-access-my-package-over-http

Again, SwiftUI is not my area of knowledge, but I think that details how you could use something like the following to invoke your deployed package:

import SwiftUI

struct ContentView: View {
    @State private var results: [String] = []

    func fetchData() {
        // 1. Create URL object
        guard let url = URL(string: "https://{userHandle}.steamship.run/{workspaceHandle}/{instanceHandle}/{apiVerb}") else {
            print("Invalid URL")
            return
        }

        // 2. Create URL request object with authentication header
        var request = URLRequest(url: url)
        request.addValue("Bearer {apiKey}", forHTTPHeaderField: "Authorization")

        // 3. Perform the request
        URLSession.shared.dataTask(with: request) { data, response, error in
            // 4. Handle the response
            guard let data = data else {
                print("No data in response: \(error?.localizedDescription ?? "Unknown error")")
                return
            }

            if let decodedResponse = try? JSONDecoder().decode([String].self, from: data) {
                // 5. Update the UI on the main thread
                DispatchQueue.main.async {
                    self.results = decodedResponse
                }
                return
            }

            print("Invalid response")
        }.resume()
    }

    var body: some View {
        VStack {
            Button("Fetch Data") {
                fetchData()
            }
            List(results, id: \.self) { result in
                Text(result)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

thanks to ChatGPT for the Swift code generation.

You'd obviously need to replace the stuff in brackets with the details that pertain to you. So, your URL would look something like:

https://alelordelo.steamship.run/default/my-instance/generate

alelordelo commented 1 year ago

thanks a lot @douglas-reid for such a detailed reply! : )

Will did deep into everything this week!

douglas-reid commented 1 year ago

@alelordelo haven't heard any more here. I assume that this is now resolved. I'm going to close this for now. Please feel free to reopen if you are still having issues.