๐Ÿซ CAMEL๋กœ Mistral ๋ชจ๋ธ๊ณผ ๊ทธ๋ž˜ํ”„ RAG ์ˆ˜ํ–‰ํ•˜๊ธฐ

๐Ÿซ CAMEL๋กœ Mistral ๋ชจ๋ธ๊ณผ ๊ทธ๋ž˜ํ”„ RAG ์ˆ˜ํ–‰ํ•˜๊ธฐ (Using CAMEL to Do Graph RAG with Mistral Models)

CAMEL์„ ์‚ฌ์šฉํ•ด ๊ทธ๋ž˜ํ”„ ๊ธฐ๋ฐ˜ RAG(Retrieval-Augmented Generation)๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šฐ๋Š” ๋ฌธ์„œ์˜ˆ์š”. Mistral Large 2 ๋ชจ๋ธ๋กœ ์ฝ˜ํ…์ธ ์—์„œ ์ง€์‹์„ ์ถ”์ถœยท๊ตฌ์กฐํ™”ํ•ด Neo4j ๊ทธ๋ž˜ํ”„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜๊ณ , ๋ฒกํ„ฐ ๊ฒ€์ƒ‰๊ณผ ์ง€์‹ ๊ทธ๋ž˜ํ”„ ๊ฒ€์ƒ‰์„ ๊ฒฐํ•ฉํ•œ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ๋ฐฉ์‹์œผ๋กœ ์ฟผ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์ถœ์ฒ˜: ๋ฌธ์„œ

๋ณธ๋ฌธ

์ด ์ฟก๋ถ์€ CAMEL์„ ์‚ฌ์šฉํ•ด ๊ทธ๋ž˜ํ”„ ๊ธฐ๋ฐ˜ RAG๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ณผ์ •์„ ์•ˆ๋‚ดํ•ด์š”. ํŠนํžˆ Mistral Large 2 ๋ชจ๋ธ๋กœ ์ฃผ์–ด์ง„ ์ฝ˜ํ…์ธ  ์†Œ์Šค์—์„œ ์ง€์‹์„ ์ถ”์ถœยท๊ตฌ์กฐํ™”ํ•ด Neo4j ๊ทธ๋ž˜ํ”„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜๊ณ , ์ดํ›„ ๋ฒกํ„ฐ ๊ฒ€์ƒ‰๊ณผ ์ง€์‹ ๊ทธ๋ž˜ํ”„ ๊ฒ€์ƒ‰์„ ๊ฒฐํ•ฉํ•œ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ ‘๊ทผ์œผ๋กœ ์ €์žฅ๋œ ์ง€์‹์„ ์ฟผ๋ฆฌยทํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์–ด์š”.

๐Ÿ“ฆ ์„ค์น˜ (Installation)

๋จผ์ € CAMEL ํŒจํ‚ค์ง€๋ฅผ ๋ชจ๋“  ์˜์กด์„ฑ๊ณผ ํ•จ๊ป˜ ์„ค์น˜ํ•ด์š”.

pip install camel-ai[all]==0.1.6.0

๐Ÿ”ง ์„ค์ • (Setup)

CAMEL-AI์—์„œ ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธํ•ด์š”.

from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.configs import MistralConfig, OllamaConfig
from camel.loaders import UnstructuredIO
from camel.storages import Neo4jGraph
from camel.retrievers import AutoRetriever
from camel.embeddings import MistralEmbedding
from camel.types import StorageType, RoleType
from camel.agents import ChatAgent, KnowledgeGraphAgent
from camel.messages import BaseMessage

๐Ÿ”‘ API ํ‚ค ์„ค์ • (Setting Up API Keys)

Mistral AI ์„œ๋น„์Šค์— ์•ˆ์ „ํ•˜๊ฒŒ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด API ํ‚ค๋ฅผ ํ”„๋กฌํ”„ํŠธ๋กœ ์ž…๋ ฅ๋ฐ›์•„์š”.

import os
from getpass import getpass

# Prompt for the API key securely
mistral_api_key = getpass('Enter your API key: ')
os.environ["MISTRAL_API_KEY"] = mistral_api_key

๐Ÿ—„๏ธ Neo4j ๊ทธ๋ž˜ํ”„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์ •

URL, ์‚ฌ์šฉ์ž ์ด๋ฆ„, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ œ๊ณตํ•ด Neo4j ์ธ์Šคํ„ด์Šค๋ฅผ ์„ค์ •ํ•ด์š”. [์—ฌ๊ธฐ]์— ์•ˆ๋‚ด๊ฐ€ ์žˆ๊ณ , ๋‹ค์šด๋กœ๋“œํ•œ .txt ํŒŒ์ผ์—์„œ ์ž๊ฒฉ ์ฆ๋ช…์„ ํ™•์ธํ•˜์„ธ์š”. ๋ฐฉ๊ธˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์„ค์ •ํ–ˆ๋‹ค๋ฉด ์ตœ๋Œ€ 60์ดˆ๋ฅผ ๊ธฐ๋‹ค๋ ค์•ผ ํ•  ์ˆ˜ ์žˆ์–ด์š”.

# Set Neo4j instance
n4j = Neo4jGraph(
    url="Your_URI",
    username="Your_Username",
    password="Your_Password",
)

๐Ÿง  ๋ชจ๋ธ ์ƒ์„ฑ (Creating the Model)

CAMEL ModelFactory๋กœ Mistral Large 2 ๋ชจ๋ธ์„ ์„ค์ •ํ•ด์š”.

# Set up model
mistral_large_2 = ModelFactory.create(
    model_platform=ModelPlatformType.MISTRAL,
    model_type=ModelType.MISTRAL_LARGE,
    model_config_dict=MistralConfig(temperature=0.2).__dict__,
)
# You can also set up model locally by using ollama
mistral_large_2_local = ModelFactory.create(
    model_platform=ModelPlatformType.OLLAMA,
    model_type="mistral-large",
    model_config_dict=OllamaConfig(temperature=0.2).__dict__,
)

๐Ÿค– CAMEL ์—์ด์ „ํŠธ๋กœ ์ง€์‹ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ

์ง€์‹ ๊ทธ๋ž˜ํ”„ ์—์ด์ „ํŠธ ์ธ์Šคํ„ด์Šค๋ฅผ ์„ค์ •ํ•ด์š”.

# Set instance
uio = UnstructuredIO()
kg_agent = KnowledgeGraphAgent(model=mistral_large_2)

์ง€์‹ ๊ทธ๋ž˜ํ”„ ์—์ด์ „ํŠธ๊ฐ€ ์ฒ˜๋ฆฌํ•  ์˜ˆ์‹œ ํ…์ŠคํŠธ ์ž…๋ ฅ์„ ์ œ๊ณตํ•ด์š”.

# Set example text input
text_example = """
CAMEL has developed a knowledge graph agent can run with Mistral AI's most
advanced model, the Mistral Large 2. This knowledge graph agent is capable
of extracting entities and relationships from given content and create knowledge
graphs automaticlly.
"""

ํ…์ŠคํŠธ์—์„œ ์š”์†Œ(element)๋ฅผ ๋งŒ๋“ค๊ณ  ์ง€์‹ ๊ทธ๋ž˜ํ”„ ์—์ด์ „ํŠธ๋กœ ๋…ธ๋“œยท๊ด€๊ณ„ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•ด์š”.

# Create an element from given text
element_example = uio.create_element_from_text(text=text_example)
# Let Knowledge Graph Agent extract node and relationship information
ans_element = kg_agent.run(element_example, parse_graph_elements=False)
print(ans_element)
# Check graph element
graph_elements = kg_agent.run(element_example, parse_graph_elements=True)
print(graph_elements)

์ถ”์ถœ๋œ ๊ทธ๋ž˜ํ”„ ์š”์†Œ๋ฅผ Neo4j ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ถ”๊ฐ€ํ•ด์š”.

# Add the element to neo4j database
n4j.add_graph_elements(graph_elements=[graph_elements])

๐ŸŽ‰ ์ด์ œ [์—ฌ๊ธฐ]์—์„œ CAMEL์˜ Knowledge Graph Agent์™€ Mistral AI์˜ Mistral Large 2 ๋ชจ๋ธ๋กœ ๋งŒ๋“  ์ง€์‹ ๊ทธ๋ž˜ํ”„๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”!

๐Ÿ—ƒ๏ธ CAMEL๋กœ ๊ทธ๋ž˜ํ”„ RAG ์‹คํ–‰ (Running Graph RAG with CAMEL)

๋ฒกํ„ฐ ๊ฒ€์ƒ‰๊ณผ ์ง€์‹ ๊ทธ๋ž˜ํ”„ ๊ฒ€์ƒ‰์„ ๊ฒฐํ•ฉํ•œ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ๋ฐฉ์‹์œผ๋กœ RAG๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ๋“œ๋ฆด๊ฒŒ์š”. Mistral AI์˜ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ๊ณผ ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฒกํ„ฐ ๊ฒ€์ƒ‰๊ธฐ๋ฅผ ์„ค์ •ํ•ด์š”.

# Set retriever
camel_retriever = AutoRetriever(
    vector_storage_local_path="local_data/embedding_storage",
    storage_type=StorageType.QDRANT,
    embedding_model=MistralEmbedding(),
)

์˜ˆ์‹œ ์‚ฌ์šฉ์ž ์ฟผ๋ฆฌ๋ฅผ ์ œ๊ณตํ•ด์š”.

# Set one user query
query="what's the relationship between Mistral Large 2 and Mistral AI? What kind of feature does Mistral Large 2 has?"

๋ฒกํ„ฐ ๊ฒ€์ƒ‰๊ธฐ๋กœ ๊ด€๋ จ ์ฝ˜ํ…์ธ ๋ฅผ ๊ฒ€์ƒ‰ํ•ด์š”. ์—ฌ๊ธฐ์„œ๋Š” Mistral AI ์›น์‚ฌ์ดํŠธ์˜ ๋‰ด์Šค๋ฅผ ์˜ˆ์‹œ ์ฝ˜ํ…์ธ ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋กœ์ปฌ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์–ด์š”.

# Get related content by using vector retriever
vector_result = camel_retriever.run_vector_retriever(
    query=query,
    content_input_paths="https://mistral.ai/news/mistral-large-2407/",
)

# Show the result from vector search
print(vector_result)

์ง€์ •๋œ URL์—์„œ ์ฝ˜ํ…์ธ ๋ฅผ ํŒŒ์‹ฑํ•˜๊ณ  ์ง€์‹ ๊ทธ๋ž˜ํ”„ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์š”.

# Parse conetent from mistral website and create knowledge graph data by using
# the Knowledge Graph Agent, store the information into graph database.

elements = uio.parse_file_or_url(
    input_path="https://mistral.ai/news/mistral-large-2407/"
)
chunk_elements = uio.chunk_elements(
    chunk_type="chunk_by_title", elements=elements
)

graph_elements = []
for chunk in chunk_elements:
    graph_element = kg_agent.run(chunk, parse_graph_elements=True)
    n4j.add_graph_elements(graph_elements=[graph_element])
    graph_elements.append(graph_element)

์‚ฌ์šฉ์ž ์ฟผ๋ฆฌ์—์„œ ์š”์†Œ๋ฅผ ๋งŒ๋“ค์–ด์š”.

# Create an element from user query
query_element = uio.create_element_from_text(text=query)

# Let Knowledge Graph Agent extract node and relationship information from the qyery
ans_element = kg_agent.run(query_element, parse_graph_elements=True)

์ฟผ๋ฆฌ์—์„œ ์–ป์€ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ง€์‹ ๊ทธ๋ž˜ํ”„ ์Šคํ† ๋ฆฌ์ง€ ์ฝ˜ํ…์ธ ์—์„œ ๋งค์นญํ•ด์š”. Neo4j์˜ Cypher ์ฟผ๋ฆฌ๋กœ ๊ด€๋ จ ๋…ธ๋“œ์™€ ๊ด€๊ณ„๋ฅผ ์กฐํšŒํ•ด์š”.

# Match the enetity got from query in the knowledge graph storage content
kg_result = []
for node in ans_element.nodes:
    n4j_query = f"""
MATCH (n {{id: '{node.id}'}})-[r]->(m)
RETURN 'Node ' + n.id + ' (label: ' + labels(n)[0] + ') has relationship ' + type(r) + ' with Node ' + m.id + ' (label: ' + labels(m)[0] + ')' AS Description
UNION
MATCH (n)<-[r]-(m {{id: '{node.id}'}})
RETURN 'Node ' + m.id + ' (label: ' + labels(m)[0] + ') has relationship ' + type(r) + ' with Node ' + n.id + ' (label: ' + labels(n)[0] + ')' AS Description
"""
    result = n4j.query(query=n4j_query)
    kg_result.extend(result)

kg_result = [item['Description'] for item in kg_result]

# Show the result from knowledge graph database
print(kg_result)

๋ฒกํ„ฐ ๊ฒ€์ƒ‰๊ณผ ์ง€์‹ ๊ทธ๋ž˜ํ”„ ์—”ํ‹ฐํ‹ฐ ๊ฒ€์ƒ‰์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํ•ฉํ•ด์š”.

# combine result from vector seach and knowledge graph entity search
comined_results = vector_result + "\n".join(kg_result)

๊ฒ€์ƒ‰๋œ ์ปจํ…์ŠคํŠธ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์งˆ๋ฌธ์— ๋‹ตํ•˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ ์—์ด์ „ํŠธ๋ฅผ ์„ค์ •ํ•ด์š”.

# Set agent
sys_msg = BaseMessage.make_assistant_message(
    role_name="CAMEL Agent",
    content="""You are a helpful assistant to answer question,
    I will give you the Original Query and Retrieved Context,
    answer the Original Query based on the Retrieved Context.""",
)

camel_agent = ChatAgent(system_message=sys_msg,
    model=mistral_large_2)

# Pass the retrieved infomation to agent
user_prompt=f"""
The Original Query is {query}
The Retrieved Context is {comined_results}
"""

user_msg = BaseMessage.make_user_message(
    role_name="CAMEL User", content=user_prompt
)

# Get response
agent_response = camel_agent.step(user_msg)

print(agent_response.msg.content)

๐ŸŒŸ ํ•˜์ด๋ผ์ดํŠธ (Highlights)

  • ์ž๋™ํ™”๋œ ์ง€์‹ ์ถ”์ถœ: Knowledge Graph Agent๊ฐ€ ์—”ํ‹ฐํ‹ฐ์™€ ๊ด€๊ณ„ ์ถ”์ถœ์„ ์ž๋™ํ™”ํ•ด ๊ณผ์ •์„ ํšจ์œจ์ ์ด๊ณ  ํšจ๊ณผ์ ์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • Mistral AI ํ†ตํ•ฉ: Mistral AI์˜ ๊ณ ๊ธ‰ ๋ชจ๋ธ, ํŠนํžˆ Mistral Large 2๋ฅผ CAMEL-AI์™€ ํ†ตํ•ฉํ•ด ๊ฐ•๋ ฅํ•œ ์ง€์‹ ๊ทธ๋ž˜ํ”„ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
  • ์•ˆ์ „ํ•˜๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅ: CAMEL-AI์˜ ๊ฒฌ๊ณ ํ•œ ์•„ํ‚คํ…์ฒ˜์™€ ๊ทธ๋ž˜ํ”„ ์ €์žฅ์šฉ Neo4j๋ฅผ ์‚ฌ์šฉํ•ด ์•ˆ์ „ํ•˜๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์†”๋ฃจ์…˜์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.

์ด ์ฟก๋ถ์„ ๋”ฐ๋ฅด๋ฉด CAMEL AI์™€ Mistral AI์˜ ์ตœ์ฒจ๋‹จ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•ด ์ •๊ตํ•œ ์ง€์‹ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ , ๊ณ ๊ธ‰ ๋ฐ์ดํ„ฐ ๋ถ„์„๊ณผ ๊ฒ€์ƒ‰ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์–ด์š”.

๋” ์•Œ์•„๋ณด๊ธฐ (Learn more)