LangChain Swift Project Introduction
LangChain Swift is an innovative client library optimized for Apple platforms including iOS, macOS, watchOS, and visionOS. This forward-thinking project allows developers to leverage the power of language models and related tools directly within their Swift applications without the need for a server, simplifying integration and deployment. It offers a versatile suite of functionalities aimed at enhancing application capabilities in natural language processing, chatbot creation, question answering, and more.
Key Features and Setup
To get started with LangChain Swift, users need to configure API keys for services like Notion, OpenAI, and others they intend to use. This setup is initiated using a simple Swift function:
LC.initSet([
"NOTION_API_KEY":"xx",
"NOTION_ROOT_NODE_ID":"xx",
"OPENAI_API_KEY":"xx",
"OPENAI_API_BASE":"xx",
])
Functionality Overview
Local Model Support
LangChain allows for running models locally by utilizing local branches and dependencies from specific repositories, ensuring quick and efficient model inference:
Task {
if let modelPath = Bundle.main.path(forResource: "stablelm-3b-4e1t-Q4_K_M", ofType: "txt") {
let local = Local(inference: .GPTNeox_gguf, modelPath: modelPath, useMetal: true)
let r = await local.generate(text: "hi")
print("🥰\(r!.llm_output!)")
} else {
print("⚠️ loss model")
}
}
Chatbot Development
Design advanced chatbots capable of not only simple interactions but also complex discussions and assistance:
let chatgpt_chain = LLMChain(llm: OpenAI(), prompt: prompt, memory: ConversationBufferWindowMemory())
Task {
var input = "I want you to act as a Linux terminal. My first command is pwd."
var res = await chatgpt_chain.predict(args: ["human_input": input])
print("🌈:" + res!)
}
QA Bot
The QA bot effectively manages text data to answer user questions by indexing and searching through it:
Task {
let loader = TextLoader(file_path: "state_of_the_union.txt")
let documents = await loader.load()
let s = Supabase(embeddings: OpenAIEmbeddings())
for text in documents {
let docs = text_splitter.split_text(text: text.page_content)
for doc in docs {
await s.addText(text: doc)
}
}
}
Retrievers and Agents
Use retrievers for fetching information from sources like Wikipedia and implement agents to perform actions, such as querying the weather.
let agent = initialize_agent(llm: OpenAI(), tools: [WeatherTool()])
Task {
let res = await agent.run(args: "Query the weather of this week")
switch res {
case Parsed.str(let str):
print("🌈:" + str)
default: break
}
}
Advanced Tools and Extensions
LangChain Swift supports a comprehensive array of tools and parsers designed to extend the utility of language models:
- Text Splitter
- Document Loader
- Various Output Parsers
- Embedding and Vectorstore Integrations
Community and Contribution
LangChain Swift encourages community involvement and is open to developers' contributions through new features, documentation improvements, or code enhancements. Community members can discuss ideas, join the Slack channel, or contribute directly via GitHub.
LangChain Swift stands as a robust platform streamlining the integration of AI and language models into Swift-based applications, offering developers a powerful toolkit to expand their application's interactive and processing capabilities.