Published on

Langchain Prompt 기본 설정 및 실행하는 방법

Authors

LangChain은 LLM을 기반으로 한 체인 구조를 활용하여 복잡한 워크플로우를 구현할 수 있게 도와주는 라이브러리입니다.

기존에 ChatGPT API(openAI API)는 적더라도 코스트가 발생해 고민이 있었는데, 로컬에서 다양한 LLM 을 간편하게 설치하고 적용할 수 있는 Ollama를 알게되었습니다.

Python 기반의 LangChain과 Ollama를 사용하여 프롬프트를 적용한 기본 LLM 체인을 구축하는 방법을 기록합니다.

Langchain Prompt-Invoke

위의 코드는 chatGPT API 사용에 비용이 드는 것이 꺼려져, Ollama의 모델을 활용했습니다.

Ollama 를 LLM로 설정하고, 프롬프트 생성후 문자열로 출력하겠습니다.

Prompt 세팅

Python 개발 전문가로서 질문에 답변하는 역할의 간단한 프롬프트를 적용합니다.

prompt_1 는 from_template 를 통해서 간단한 프롬프트를 바로 적용합니다.

template = (
    "You are an expert in Python Developer. Answer the question. <Question>: {input}"
)

# 프롬프트 세팅_1
prompt_1 = ChatPromptTemplate.from_template(template)

prompt_2는 보다 정교한 설정을 통해, input 을 명시적으로 표현하여 처리하도록 할 수 있습니다.

# 프롬프트 세팅_2
prompt_2 = ChatPromptTemplate(
    input_variables=["input"],
    messages=[
        HumanMessagePromptTemplate(
            prompt=PromptTemplate(
                input_variables=["input"],
                template=template,
            )
        )
    ],
)

SomeRole - Template

여기서 사용된 HumanMessagePromptTemplate 클래스는 프롬프트 모듈에 존재하며, 소스코드에 아래처럼 추상화 되어 사용할 수 있는 클래스입니다.


class HumanMessagePromptTemplate(_StringImageMessagePromptTemplate):
    """Human message prompt template. This is a message sent from the user."""

    _msg_class: Type[BaseMessage] = HumanMessage

class AIMessagePromptTemplate(_StringImageMessagePromptTemplate):
    """AI message prompt template. This is a message sent from the AI."""

    _msg_class: Type[BaseMessage] = AIMessage

    @classmethod
    def get_lc_namespace(cls) -> List[str]:
        """Get the namespace of the langchain object."""
        return ["langchain", "prompts", "chat"]

HumanMessageAIMessageBaseMessage 를 상속받은 클래스로 type으로 human 인지, ai 인지 명시하는 부분이 있었고 이를 통해서 프롬프트 작성시 외부 요인에 의해 프롬프트를 의도와 어긋나게 사용하지 않도록 설계되었습니다.

BaseMessage 의 __add__ 에서 + 연산자를 오버로딩하여, BaseMessage 객체와 다른 객체를 더할 수 있게 했고, selfChatPromptTemplate에 래핑한 뒤, 다른 객체와 더하여 새로운 ChatPromptTemplate을 반환하여 프롬프트 확장을 매우(?) 염두하고 설계한 것으로 보였습니다.

LLM 및 파서 설정

llm = Ollama(model="llama2:latest")
str_output = StrOutputParser()

Ollama의 최신 llama2 모델을 사용하여 LLM을 초기화합니다.

StrOutputParser는 LLM의 출력을 단순한 문자열로 파싱하여 후속 작업에 사용할 수 있도록 합니다.

체인 구성 및 실행


chain_1 = prompt_1 | llm | str_output
response_1 = chain_1.invoke({"input": "django 와 fastapi 에 대해서 알려줘"})
print("response 1 : ", response_1)
print("*" * 100)
chain_2 = prompt_2 | llm | str_output
response_2 = chain_2.invoke({"input": "django 와 fastapi 에 대해서 알려줘"})
print("response 2 : ", response_2)

여기서는 두 개의 체인을 구성하고 실행합니다.

각 체인은 동일한 질문("django 와 fastapi 에 대해서 알려줘")에 대해 다른 방식으로 응답합니다. 이 과정에서 LLM의 출력이 어떻게 달라지는지 확인할 수 있습니다.

결론

특정 도메인에 맞는 고도화된 질문 응답 시스템을 구현해야한다면 프롬프트 작성은 필수입니다.

꽤 오랜시간이 걸리긴했지만, 출력된 모습이 생각보다 정교하게 느껴집니다.

두 출력자체의 퀄리티는 비슷한 수준으로 보아 프롬프트나 역할설정이 간결한 수준이라면, 출력자체도 비슷한 것 같습니다. 챗이나 고도화된 수준에서는 세부적인 역할 조정이 중요하게 작용할 것으로 보여집니다.

response 1 :
Certainly! Django and FastAPI are both popular Python web frameworks, but they have different design principles and use cases. Here's a brief comparison:

Django:

Pros:

1. High-level framework: Django is a mature and well-established framework that provides a lot of built-in functionality, making it easier to build web applications quickly.
2. ORM (Object-Relational Mapping): Django provides an ORM system that simplifies the process of interacting with databases, allowing developers to focus on the application logic rather than the database.
3. Modular architecture: Django has a modular architecture that allows developers to build applications in smaller, more manageable pieces, making it easier to maintain and scale.
4. Large community: Django has a large and active community of developers, which means there are many resources available for learning and troubleshooting.

Cons:

1. Steep learning curve: Django can be challenging to learn, especially for beginners, due to its complex syntax and numerous features.
2. Overkill for small projects: Django is designed for large-scale applications, so it may not be the best choice for smaller projects that don't require its full feature set.
3. Slow development: Due to Django's complexity, developers may find themselves spending more time setting up the framework than actually building the application.

FastAPI:

Pros:

1. Fast and lightweight: FastAPI is designed to be fast and lightweight, making it ideal for small to medium-sized applications.
2. Simplified architecture: FastAPI has a simplified architecture that makes it easier to learn and use, especially for beginners.
3. Easy integration with existing projects: FastAPI can easily integrate with existing Python projects, making it a good choice for developers who want to add web functionality to their existing codebase.
4. Modern design: FastAPI is designed with modern software design principles in mind, making it more flexible and adaptable than older frameworks like Django.

Cons:

1. Limited functionality: FastAPI is a relatively new framework, so it doesn't have as many built-in features as Django. This means developers may need to write more code to accomplish certain tasks.
2. Smaller community: FastAPI has a smaller community of developers compared to Django, which can make it harder to find resources and troubleshoot issues.
3. Less mature ecosystem: FastAPI's ecosystem is still developing, so there may be fewer third-party libraries and tools available compared to Django.

In summary, Django is a more established and comprehensive framework that provides a lot of built-in functionality, but it can also be slower and more complex to use. FastAPI is a newer and more lightweight framework that simplifies the development process, but it may not have as many features or resources available yet. The choice between the two ultimately depends on the specific needs and goals of the project.
****************************************************************************************************
response 2 :
Of course! Django and FastAPI are both popular web frameworks for building web applications using Python. Here's a brief comparison between the two:

Django:

Pros:

1. Maturity: Django is an older framework, first released in 2005, which means it has a more established community and a wider range of libraries and tools available.
2. Ease of use: Django's syntax is easier to learn for beginners, with a simpler structure and more intuitive API.
3. Security: Django has built-in security features such as authentication and access control, which can help protect your application from common web attacks.
4. Community support: Django has a large and active community, with numerous resources available online, including documentation, tutorials, and forums.
5. Rapid development: Django's high-level framework makes it easier to build complex applications quickly.

Cons:

1. Overkill: Django is a full-featured framework that may be overkill for small projects or simple web applications.
2. Steep learning curve: While Django's syntax is easier than some other frameworks, it can still take time to learn and master.
3. Performance overhead: Django's overhead can be higher than some other frameworks due to its extensive library and the use of an ORM (Object-Relational Mapping) system.

FastAPI:

Pros:

1. Lightweight: FastAPI is a modern, lightweight framework that is designed for high-performance applications.
2. Ease of development: FastAPI has a minimalist design philosophy, which means it requires less code to build a web application.
3. Flexibility: FastAPI is built on top of Starlette, which provides a flexible and modular structure.
4. Performance: FastAPI's lightweight design and lack of overhead make it an excellent choice for high-performance applications.
5. Easy integration: FastAPI can easily integrate with other Python libraries and frameworks, such as Django or Flask.

Cons:

1. Learning curve: FastAPI is a relatively new framework, so there may be a steeper learning curve for beginners.
2. Limited community support: While the FastAPI community is growing, it may not have as much documentation and resources available as Django.
3. Security features: FastAPI has fewer built-in security features than Django, which means you may need to implement your own authentication and access control mechanisms.

In summary, Django is a mature and feature-rich framework that is ideal for large and complex applications, while FastAPI is a lightweight and flexible framework that is better suited for smaller projects or those requiring high performance. Ultimately, the choice between the two will depend on your specific needs and preferences.

Process finished with exit code 0

hongreat 블로그의 글을 봐주셔서 감사합니다!