https://github.com/wangmuy/llmchain
LangChain for android/Java/JVM/Kotlin Multiplatform, using OpenAI chatGPT compatible APIs
https://github.com/wangmuy/llmchain
Last synced: about 1 month ago
JSON representation
LangChain for android/Java/JVM/Kotlin Multiplatform, using OpenAI chatGPT compatible APIs
- Host: GitHub
- URL: https://github.com/wangmuy/llmchain
- Owner: wangmuy
- License: apache-2.0
- Created: 2023-04-30T15:09:19.000Z (about 3 years ago)
- Default Branch: multiplatform
- Last Pushed: 2023-10-03T00:00:57.000Z (almost 3 years ago)
- Last Synced: 2023-10-03T09:33:01.044Z (almost 3 years ago)
- Language: Kotlin
- Homepage:
- Size: 296 KB
- Stars: 13
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-java - LLMChain
README
# LLMChain
This is an experimental port of [langchain( currently v0.0.139 )](https://github.com/hwchase17/langchain/tree/v0.0.139) to android/JVM/Kotlin Multiplatform.
Please note that this project is currently in the proof-of-concept stage, and is subject to change.
## Maven repository
Only the SNAPSHOT version is published.
Add to repositories
```gradle
repositories {
mavenCentral()
maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/" }
}
```
Add to dependencies
* Android/JVM developers are advised to use the android branch README dependency directions.
* For Kotlin Multiplatform developers, try to add the following
```gradle
dependencies {
implementation("io.github.wangmuy.llmchain.kmp:core:0.0.1-SNAPSHOT") { changing=true }
implementation("io.github.wangmuy.llmchain.kmp:serviceprovider-openai:0.0.1-SNAPSHOT") { changing=true }
}
```
Be sure to checkout the support matrix.
### Multiplatform support matrix
| package | platform | isCompiled | isTested |
|------------------------|----------|------------|----------|
| core | jvm | true | true |
| core | ios | false | false |
| core | native | true | true |
| core | js | true | true |
| core | wasm | true | false |
| serviceprovider-openai | jvm | true | true |
| serviceprovider-openai | ios | false | false |
| serviceprovider-openai | native | true | true |
| serviceprovider-openai | js | true | false |
| serviceprovider-openai | wasm | false | false |
## Quickstart
Here's the almost one-to-one translation of [langchain Quickstart Guide](https://python.langchain.com/docs/get_started/quickstart)
in [Quickstart.kt](https://github.com/wangmuy/llmchain/blob/main/core/src/test/kotlin/com/wangmuy/llmchain/Quickstart.kt), including all the modules/components.
### LLMs
```kotlin
val llm = OpenAIChat(APIKEY, proxy = PROXY)
llm.invocationParams[OpenAIChat.REQ_TEMPERATURE] = 0.9
val text = "What would be a good company name for a company that makes colorful socks?"
val output = llm.invoke(text, null)
println("output=\n$output")
```
### Prompt templates
```kotlin
val prompt = PromptTemplate(
inputVariables = listOf("product"),
template = "What is a good name for a company that makes {product}?")
val formatted = prompt.format(mapOf("product" to "colorful socks"))
assertEquals("What is a good name for a company that makes colorful socks?", formatted)
```
### Chains
```kotlin
val llm = OpenAIChat(APIKEY)
llm.invocationParams[OpenAIChat.REQ_TEMPERATURE] = 0.9
val prompt = PromptTemplate(
inputVariables = listOf("product"),
template = "What is a good name for a company that makes {product}?")
val chain = LLMChain(llm = llm, prompt = prompt)
val output = chain.run(mapOf("product" to "colorful socks"))
println("output=\n$output")
```
### Agents
```kotlin
val llm = OpenAIChat(APIKEY)
llm.invocationParams[OpenAIChat.REQ_TEMPERATURE] = 0.0
val fakeSerpApiTool = Tool(
name = "Search",
description = "A search engine. Useful for when you need to answer questions about current events. Input should be a search query.",
func = {_, _ -> "San Francisco Temperature Yesterday. Maximum temperature yesterday: 57 °F (at 1:56 pm) Minimum temperature yesterday: 49 °F (at 1:56 am)"}
)
val llmMathTool = LLMMathChain.asTool(llm)
val agentExecutor = Factory.initializeAgent(listOf(fakeSerpApiTool, llmMathTool), llm,
Factory.AGENT_TYPE_ZERO_SHOT_REACT_DESCRIPTION)
agentExecutor.maxIterations = 4
val output = agentExecutor.run(mapOf("input" to "What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?"))
println("output=\n$output")
```
### Memory
```kotlin
val logCallbackHandler = object: DefaultCallbackHandler() {
override fun onText(text: String, verbose: Boolean) {
println(text)
}
}
val callbackManager = CallbackManager(mutableListOf(logCallbackHandler))
val llm = OpenAIChat(APIKEY).apply {
invocationParams[OpenAIChat.REQ_MAX_TOKENS] = 50
}
llm.callbackManager = callbackManager
val conversation = ConversationChain(llm, verbose = true, callbackManager = callbackManager)
var output: Map = emptyMap()
var outputStr: String = ""
output = conversation.invoke(mapOf("input" to "Hi there!"))
outputStr = output[conversation.outputKey]!!.toString()
println("output=\n$outputStr")
output = conversation.invoke(mapOf("input" to "I'm doing well! Just having a conversation with an AI."))
```
## implementations
- Schema
- [x] nearly all basic schema interfaces
- LLM
- [x] BaseLLM
- [x] LLM
- Prompt template
- [x] BasePromptTemplate
- [x] StringPromptTemplate
- [x] StringPromptValue
- [x] PromptTemplate
- Chain
- [x] Chain
- [x] LLMChain
- [ ] LLMMathChain: currently use LLM, not the actual calculator
- [x] ConversationChain
- [x] RouterChain/MultiRouteChain/LLMRouterChain/MultiPromptChain Note: Only available in jvm/native due to limited implementation of `RegexOption.DOT_MATCHES_ALL`
- [x] SequentialChain/SimpleSequentialChain
- Agent
- [x] Agent
- [x] AgentExecutor
- [x] ZeroShotAgent
- Tools
- [x] BaseTool
- [x] Tool
- [x] InvalidTool
- Memory
- [x] SimpleMemory
- [x] ChatMemory
- DocStore
- [x] DocStore
- [x] InMemoryDocStore
- VectorStore
- [x] VectorStore
- [x] SimpleVectorStore
- [x] VectorStoreRetriever
- Embedding
- [x] Embeddings
- LLM service provider
- [x] OpenAI
- [x] OpenAIChat
- [x] OpenAIEmbedding
- [x] Function Calling
## License
```text
Copyright 2023 wangmuy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```