Skip to content

Commit

Permalink
Improve the langchain example
Browse files Browse the repository at this point in the history
Signed-off-by: SimFG <bang.fu@zilliz.com>
  • Loading branch information
SimFG committed Apr 14, 2023
1 parent 638dbe9 commit 3650b32
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 23 deletions.
80 changes: 80 additions & 0 deletions docs/release_note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# 🎉 Introduction to new functions of GPTCache

To read the following content, you need to understand the basic use of GPTCache, references:

- [Readme doc](https://github.com/zilliztech/GPTCache)
- [Usage doc](https://github.com/zilliztech/GPTCache/blob/main/docs/usage.md)

## v0.1.10 (2023.4.13)

1. Add kreciprocal similarity evaluation

K-reprciprocl evaluation is a method inspired by the popular reranking method in ReID(https://arxiv.org/abs/1701.08398). The term “k-reciprocal” comes from the fact that the algorithm creates reciprocal relationships between similar embeddings in the top-k list. In other words, if embedding A is similar to embedding B and embedding B is similar to embedding A, then A and B are said to be “reciprocally similar” to each other. This evaluation abandon those embeddings pairs which are not “reciprocally similar” in their K nearest neighbors. And the remaining pairs will keep the distance for the final rank.

```
vector_base = VectorBase("faiss", dimension=d)
data_manager = get_data_manager(CacheBase("sqlite"), vector_base)
evaluation = KReciprocalEvaluation(vectordb=vector_base)
cache.init(
... # other configs
data_manager=data_manager,
similarity_evaluation=evaluation,
)
```

2. Add LangChainChat adapter

```
from gptcache.adapter.langchain_models import LangChainChat
cache.init(
pre_embedding_func=get_msg,
)
chat = LangChainChat(chat=ChatOpenAI(temperature=0))
answer = chat(
messages=[
HumanMessage(
content="Translate this sentence from English to Chinese. I love programming."
)
]
)
```

## v0.1.9 (2023.4.12)

1. Import data into cache

```
cache.init()
questions = ["foo1", "foo2"]
answers = ["a1", "a2"]
cache.import_data(questions=questions, answers=answers)
```

2. New pre-process function: remove prompts

When using the LLM model, a prompt may be added for each input. If the entire message with the prompt is brought into the cache, it may lead to an increase in the cache error hit rate. For example, the text of the prompt is very long, and the text of the real question is very short. .

```
cache_obj.init(
pre_embedding_func=last_content_without_prompt,
config=Config(prompts=["foo"]),
)
```

3. Embeded milvus

The embedded Milvus is a lightweight version of Milvus that can be embedded into your Python application. It is a single binary that can be easily installed and run on your machine.

```
with TemporaryDirectory(dir="./") as root:
db = VectorBase(
"milvus",
local_mode=True,
local_data=str(root),
... #other config
)
data_manager = get_data_manager("sqlite", vector_base)
```
20 changes: 12 additions & 8 deletions examples/adapter/langchain_llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ def run_llm():
pre_embedding_func=get_prompt,
)

question = 'what is chatgpt'
question = "what is chatgpt"

langchain_openai = OpenAI(model_name='text-ada-001')
langchain_openai = OpenAI(model_name="text-ada-001")
llm = LangChainLLMs(llm=langchain_openai)
answer = llm(question)
answer = llm(prompt=question)
print(answer)

# TODO install cohere auto
langchain_cohere = Cohere()
llm = LangChainLLMs(llm=langchain_cohere)
answer = llm(question)
answer = llm(prompt=question)
print(answer)


Expand All @@ -43,13 +43,17 @@ def run_chat_model():
pre_embedding_func=get_msg,
)

# chat=ChatOpenAI(temperature=0)
chat = LangChainChat(chat=ChatOpenAI(temperature=0))

answer = chat([HumanMessage(content="Translate this sentence from English to Chinese. I love programming.")])
answer = chat(
messages=[
HumanMessage(
content="Translate this sentence from English to Chinese. I love programming."
)
]
)
print(answer)


if __name__ == '__main__':
if __name__ == "__main__":
run_llm()
run_chat_model()
15 changes: 2 additions & 13 deletions examples/langchain_examples/langchain_prompt_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,11 @@

before = time.time()
cached_llm = LangChainLLMs(llm=llm)
answer = cached_llm(question, cache_obj=llm_cache)
answer = cached_llm(prompt=question, cache_obj=llm_cache)
print(answer)
print("Read through Time Spent =", time.time() - before)

before = time.time()
answer = cached_llm(question, cache_obj=llm_cache)
print(answer)
print("Cache Hit Time Spent =", time.time() - before)

before = time.time()
answer = cached_llm("What is your hobby", cache_obj=llm_cache)
print(answer)
print("Read through Time Spent =", time.time() - before)

before = time.time()
question = "What is the winner Super Bowl in the year Justin Bieber was born?"
answer = cached_llm(question, cache_obj=llm_cache)
answer = cached_llm(prompt=question, cache_obj=llm_cache)
print(answer)
print("Cache Hit Time Spent =", time.time() - before)
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@

before = time.time()
cached_llm = LangChainLLMs(llm=llm)
answer = cached_llm(question, cache_obj=llm_cache)
answer = cached_llm(prompt=question, cache_obj=llm_cache)
print(answer)
print("Read through Time Spent =", time.time() - before)

before = time.time()
question = "What is the winner Super Bowl in the year Justin Bieber was born?"
answer = cached_llm(question, cache_obj=llm_cache)
answer = cached_llm(prompt=question, cache_obj=llm_cache)
print(answer)
print("Cache Hit Time Spent =", time.time() - before)

0 comments on commit 3650b32

Please sign in to comment.