Skip to content

Commit

Permalink
Merge pull request HamaWhiteGG#138 from HamaWhiteGG/dev
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
HamaWhiteGG authored Nov 30, 2023
2 parents c2b1240 + 1984c3c commit ae9059f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ This is the Java language implementation of LangChain, which makes it as easy as

The following example in the [langchain-example](langchain-examples/src/main/java/com/hw/langchain/examples).

- [SQL Chains](langchain-examples/src/main/java/com/hw/langchain/examples/chains/SqlChainExample.java)
- [API Chains](langchain-examples/src/main/java/com/hw/langchain/examples/chains/ApiChainExample.java)
- [SQL Chain](langchain-examples/src/main/java/com/hw/langchain/examples/chains/SqlChainExample.java)
- [API Chain](langchain-examples/src/main/java/com/hw/langchain/examples/chains/ApiChainExample.java)
- [RAG Milvus](langchain-examples/src/main/java/com/hw/langchain/examples/chains/MilvusExample.java)
- [RAG Pinecone](langchain-examples/src/main/java/com/hw/langchain/examples/chains/RetrievalQaExample.java)
- [Summarization](langchain-examples/src/main/java/com/hw/langchain/examples/chains/SummarizationExample.java)
Expand Down Expand Up @@ -80,6 +80,7 @@ var llm = OpenAI.builder()
### 3.3 LLMs
Get predictions from a language model. The basic building block of LangChain is the LLM, which takes in text and generates more text.

[OpenAI Example](langchain-examples/src/main/java/com/hw/langchain/examples/llms/OpenAIExample.java)
```java
var llm = OpenAI.builder()
.temperature(0.9f)
Expand All @@ -97,7 +98,7 @@ Feetful of Fun

Chat models are a variation on language models. While chat models use language models under the hood, the interface they expose is a bit different: rather than expose a "text in, text out" API, they expose an interface where "chat messages" are the inputs and outputs.


[OpenAI Chat Example](langchain-examples/src/main/java/com/hw/langchain/examples/chat/models/ChatExample.java)
```java
var chat = ChatOpenAI.builder()
.temperature(0)
Expand All @@ -113,7 +114,6 @@ AIMessage{content='J'adore la programmation.', additionalKwargs={}}
```
It is useful to understand how chat models are different from a normal LLM, but it can often be handy to just be able to treat them the same. LangChain makes that easy by also exposing an interface through which you can interact with a chat model as you would a normal LLM. You can access this through the `predict` interface.
```java
var output = chat.predict("Translate this sentence from English to French. I love programming.");
println(output);
Expand All @@ -128,7 +128,11 @@ Now that we've got a model and a prompt template, we'll want to combine the two.

#### 3.5.1 LLMs
The simplest and most common type of chain is an LLMChain, which passes an input first to a PromptTemplate and then to an LLM. We can construct an LLM chain from our existing model and prompt template.

[LLM Chain Example](langchain-examples/src/main/java/com/hw/langchain/examples/chains/LlmChainExample.java)
```java
var prompt = PromptTemplate.fromTemplate("What is a good name for a company that makes {product}?");

var chain = new LLMChain(llm, prompt);
var result = chain.run("colorful socks");
println(result);
Expand All @@ -138,7 +142,14 @@ Feetful of Fun
```
#### 3.5.2 Chat models
The `LLMChain` can be used with chat models as well:

[LLM Chat Chain Example](langchain-examples/src/main/java/com/hw/langchain/examples/chains/ChatChainExample.java)
```java
var template = "You are a helpful assistant that translates {input_language} to {output_language}.";
var systemMessagePrompt = SystemMessagePromptTemplate.fromTemplate(template);
var humanMessagePrompt = HumanMessagePromptTemplate.fromTemplate("{text}");
var chatPrompt = ChatPromptTemplate.fromMessages(List.of(systemMessagePrompt, humanMessagePrompt));

var chain = new LLMChain(chat, chatPrompt);
var result = chain.run(Map.of("input_language", "English", "output_language", "French", "text", "I love programming."));
println(result);
Expand All @@ -147,12 +158,12 @@ println(result);
J'adore la programmation.
```
#### 3.5.3 SQL Chains Example
#### 3.5.1 SQL Chains Example
LLMs make it possible to interact with SQL databases using natural language, and LangChain offers SQL Chains to build and run SQL queries based on natural language prompts.
![SQL chains.png](https://github.com/HamaWhiteGG/langchain-java/blob/dev/data/images/SQL%20chains.png)
[SQL Chains](langchain-examples/src/main/java/com/hw/langchain/examples/chains/SqlChainExample.java)
[SQL Chain Example](langchain-examples/src/main/java/com/hw/langchain/examples/chains/SqlChainExample.java)
```java
var database = SQLDatabase.fromUri("jdbc:mysql://127.0.0.1:3306/demo", "xxx", "xxx");
Expand Down Expand Up @@ -195,7 +206,7 @@ export SERPAPI_API_KEY=xxx
To augment OpenAI's knowledge beyond 2021 and computational abilities through the use of the Search and Calculator tools.
![Google agent example.png](https://github.com/HamaWhiteGG/langchain-java/blob/dev/data/images/Google%20agent%20example.png)
[Google Search Agent](langchain-examples/src/main/java/com/hw/langchain/examples/agents/ChatAgentExample.java)
[Google Search Agent Example](langchain-examples/src/main/java/com/hw/langchain/examples/agents/ChatAgentExample.java)
```java
// the 'llm-math' tool uses an LLM
var tools = loadTools(List.of("serpapi", "llm-math"), llm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,19 @@
public class ChatAgentExample {

public static void main(String[] args) {
// The language model we're going to use to control the agent.
var chat = ChatOpenAI.builder().temperature(0).model("gpt-4").build().init();

// The tools we'll give the Agent access to. Note that the 'llm-math' tool uses an LLM, so we need to pass that
// in.
// Note that the 'llm-math' tool uses an LLM, so we need to pass that in.
var llm = OpenAI.builder().temperature(0).build().init();
var tools = loadTools(List.of("serpapi", "llm-math"), llm);

// Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
// let's initialize an agent with the tools, the language model, and the type of agent we want to use.
var agent = initializeAgent(tools, chat, AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION);

// var query = "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?";
var query = "How many countries and regions participated in the 2023 Hangzhou Asian Games?" +
"What is that number raised to the .023 power?";

// Now let's test it out!
agent.run(query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public static void main(String[] args) {

var template = "You are a helpful assistant that translates {input_language} to {output_language}.";
var systemMessagePrompt = SystemMessagePromptTemplate.fromTemplate(template);
var humanTemplate = "{text}";
var humanMessagePrompt = HumanMessagePromptTemplate.fromTemplate(humanTemplate);
var humanMessagePrompt = HumanMessagePromptTemplate.fromTemplate("{text}");
var chatPrompt = ChatPromptTemplate.fromMessages(List.of(systemMessagePrompt, humanMessagePrompt));

var chain = new LLMChain(chat, chatPrompt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void main(String[] args) {
.build()
.init();

var result = llm.predict("Introduce West Lake in Hangzhou, China.");
var result = llm.predict("What would be a good company name for a company that makes colorful socks?");
println(result);
}
}

0 comments on commit ae9059f

Please sign in to comment.